Smallpdf - 2021 11 25 Edited Edited Edited
Smallpdf - 2021 11 25 Edited Edited Edited
SESSION – 2021-22
SUBMITTED TO SUBMITTED BY
submitted by:Avinash Kumar
Bhavna B.Tech 2nd Year 3rd Sem(CS)
YOGENDRA BHAMMARKAR
B.TECH 2nd YEAR 3rd Sem(CS)
Enrollment No:0132CS201026
RITS BHOPAL Enrollment No.: 0132CS20116
0132CS201026
Page - 1
0132CS201026
Program: Assuming that Java is properly installed on your system there are three steps to
creating a Java program.
1. Open notepad and writing the code
class HelloWorld
{
public static void main (String args[])
{
System.out.println("Hello World!");
}
}
Note: 1.Save Same as the Class name.java
2. Allprogram/Run/ type cmd
. I/O standard:
To take quick input from keyboard.we used scanner class. This class comes from
java.util package.
To call Scanner in your program:
import java.util.Scanner;
Page - 2
0132CS201026
2-> Write a program that Accept two number from the user and print Sum of them ?
program:
import java.util.Scanner;
public class Addition{
// main method begins execution of Java application
public static void main( String args[] ){
Scanner input = new Scanner( System.in );
int number1;
int number2;
int sum;
System.out.print( "Enter first integer: " );
number1 = input.nextInt(); // read first number from user
System.out.print( "Enter second integer: " );
number2 = input.nextInt(); // read second number from user
sum = number1 + number2;
System.out.println( "Sum is %d\n", sum );
}
}
3-> Write a program to calculate addition of two number using prototyping of methods.
program:
class Add{
int c;
void addition(int x,int y){
c=x+y;
}
public static void main(String[] arg){
int a,b;
Scanner sc=new Scanner(System.in);
System.out.println("Enter first number");
a=sc.nextInt();
System.out.println("Enter second number");
b=sc.nextInt();
Add r=new Add();
r.addition(a,b);
System.out.println("Addition of two numbers is : "+r.c);
}
}
output:
Enter first number
5
Enter second number
1
Addition of two numbers is : 6
Page - 3
0132CS201026
Program:
5-> write a program that converts the original String to lowercase and uppercase.
Program:
class ChangeCase{
public static void main(String args[]){
String s = "This is a test.";
System.out.println("Original: " + s);
String upper = s.toUpperCase();
String lower = s.toLowerCase();
System.out.println("Uppercase: " + upper);
System.out.println("Lowercase: " + lower);
}
}
output :
Original: This is a test.
Uppercase: THIS IS A TEST.
Lowercase: this is a test.
Page - 4
0132CS201026
program:
class FindAverages{
public static void main(String s[]){
Average av = new Average();
System.out.println("Average of 7.2, 4.7 and 3.1 is " + av.average(7.2, 4.7, 3.1));
System.out.println("Average of 2.5, 40.4, 3.3 and 2.0 is " + av.averag(2.5, 40.4, 3.3, 2.0));
System.out.println("Average of 5, 10, 15, 20 and 25 is " + av.average(5, 10, 15, 20, 25));
}
}
class Average{
double average(double a, double b, double c){
return (a+b+c)/3;
}
double average(double a, double b, double c, double d){
return (a+b+c+d)/4;
}
double average(double a, double b, double c, double d, double e){
return (a+b+c+d+e)/5;
}
}
program:
class Box{
int l,b,h;
Box(int x) // Constructor for Cube
{
l=x;
b=x;
h=x;
}
Box(int x,int y,int z) //Constructor for Rectangle
{
l=x;
b=y;
h=z;
}
void volume()
Page - 5
0132CS201026
{
System.out.println("Volume of the box is "+(l*b*h));
}
public static void main(String args[])
{
Box cube= new Box(10); //Calling cube constructor
Box b=new Box(10,4,7); //Calling box constructor
cube.volume();
b.volume();
}
}
8-> Program to show the details of students using the concept of inheritance.
program:
/**
* This method is used to show details of a student.
*/
public void showDetail(){
System.out.println("Student name = " + name);
System.out.println("Student class name = " + className);
System.out.println("Student semester = " + semester);
}
}
Page - 6
0132CS201026
program:
class MultithreadingDemo extends Thread{
public void run(){
System.out.println("My thread is in running state.");
}
public static void main(String args[]){
MultithreadingDemo obj=new MultithreadingDemo();
obj.start();
}
}
Output: My thread is in running state.
Page - 7
0132CS201026
program:
class MultithreadingDemo implements Runnable{
public void run(){
System.out.println("My thread is in running state.");
}
public static void main(String args[]){
MultithreadingDemo obj=new MultithreadingDemo();
Thread tobj =new Thread(obj);
tobj.start();
}
}
Output: My thread is in running state.
Interface: The interface in Java is a mechanism to achieve abstraction. There can be only
abstract methods in the Java interface, not method body. It is used to achieve abstraction and
multiple inheritance in Java.
In other words, you can say that interfaces can have abstract methods and variables. It cannot
have a method body.
Java Interface also represents the IS-A relationship.
It cannot be instantiated just like the abstract class.
Since Java 8, we can have default and static methods in an interface.
Since Java 9, we can have private methods in an interface.
As shown in the figure given below, a class extends another class, an interface extends another
interface, but a class implements an interface.
Page - 8
0132CS201026
program:
//Interface declaration: by first user
interface Drawable{
void draw();
}
//Implementation: by second user
class Rectangle implements Drawable{
public void draw(){System.out.println("drawing rectangle");}
}
class Circle implements Drawable{
public void draw(){System.out.println("drawing circle");}
}
//Using interface: by third user
class TestInterface1{
public static void main(String args[]){
//In real scenario, object is provided by method e.g. getDrawable()
Drawable d=new Circle();
d.draw();
}
}
Output: drawing circle
Exception:
● Exception is an abnormal condition.
● An exception is an unwanted or unexpected event, which occurs during the execution of a
program i.e at run time, that disrupts the normal flow of the program’s instructions.
Exception Handling
If an exception occurs, which has not been handled by programmer then program
execution gets terminated and a system generated error message is shown to the user.
Page - 9
0132CS201026
Page - 10
0132CS201026
1) Checked Exception
The classes which directly inherit Throwable class except RuntimeException and Error are
known as checked exceptions e.g. IOException, SQLException etc. Checked exceptions are
checked at compile-time.
2) Unchecked Exception
The classes which inherit RuntimeException are known as unchecked exceptions e.g.
ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException etc. Unchecked
exceptions are not checked at compile-time, but they are checked at runtime.
3) Error
Error is irrecoverable e.g. OutOfMemoryError, VirtualMachineError, AssertionError etc.
program:
Page - 11
0132CS201026
program:
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
public class Circles extends Applet implements ActionListener{
TextField value;
Button draw;
public void init(){
resize(400, 400);
draw = new Button("Draw");
value = new TextField();
draw.addActionListener(this);
add(value);
add(draw);
}
public void actionPerformed(ActionEvent e){
Graphics g = getGraphics();
int diameter = Integer.parseInt(value.getText()) * 2;
g.drawOval(0, 0, diameter, diameter);
}
}
Output:
Page - 12