Upload
vishnu0751
View
220
Download
0
Embed Size (px)
Citation preview
7/28/2019 vitm java
1/23
Q. Write a program to print Hello JAVA
Class mpct{
Public Static void main (string arg [ ]);
{
System.out.println (Hello java);
}
}
7/28/2019 vitm java
2/23
OUTPUT
Hello Java
7/28/2019 vitm java
3/23
Q.Write a Program to show concept of CLASS in Java.
import java.io.*;public class MainClass{
public static void main( String args[] ){Account account1 = new Account( 50.00 ); // create Account objectAccount account2 = new Account( -7.53 ); // create Account objectSystem.out.printf( "account1 balance: $%.2f\n", account1.getBalance() );System.out.printf( "account2 balance: $%.2f\n\n", account2.getBalance() );double depositAmount; // deposit amount read from userdepositAmount = 10.10;account1.credit( depositAmount ); // add to account1 balanceSystem.out.printf( "account1 balance: $%.2f\n", account1.getBalance() );System.out.printf( "account2 balance: $%.2f\n\n", account2.getBalance() );depositAmount = 12.12;account2.credit( depositAmount ); // add to account2 balanceSystem.out.printf( "account1 balance: $%.2f\n", account1.getBalance() );System.out.printf( "account2 balance: $%.2f\n", account2.getBalance() );
}}class Account{
private double balance; // instance variable that stores the balance// constructor
public Account( double initialBalance ){
if ( initialBalance > 0.0 )balance = initialBalance;
}public void credit( double amount ){balance = balance + amount;
}public double getBalance()
{return balance;
}}
7/28/2019 vitm java
4/23
OUTPUT
7/28/2019 vitm java
5/23
Q. write a program to exception handling in java.
public static void main (string args[]){
int a,b;try{a=0;
b=50/a;System.out.println(this is unreacable);
catch{System.out.println(divison by zero);
}
System.out.println(after catch statement);}
}
7/28/2019 vitm java
6/23
// output //
Division by zeroAfter catch statement.
7/28/2019 vitm java
7/23
Q. Write a Program to Show Inheritance.
import java.io.*;
class Box {private double width;
private double height;
private double depth;
Box(Box ob) { // pass object to constructorwidth = ob.width;
height = ob.height;depth = ob.depth;
}
Box(double w, double h, double d) {width = w;height = h;depth = d;
}
Box() {width = -1; // use -1 to indicateheight = -1; // an uninitializeddepth = -1; // box
}
Box(double len) {width = height = depth = len;
}
double volume() {return width * height * depth;
}}
class BoxWeight extends Box {
7/28/2019 vitm java
8/23
double weight; // weight of box
BoxWeight(BoxWeight ob) { // pass object to constructorsuper(ob);weight = ob.weight;
}
BoxWeight(double w, double h, double d, double m) {super(w, h, d); // call superclass constructorweight = m;
}
BoxWeight() {super();
weight = -1;}
BoxWeight(double len, double m) {super(len);weight = m;
}}
class Shipment extends BoxWeight {double cost;
Shipment(Shipment ob) { // pass object to constructorsuper(ob);cost = ob.cost;
}
Shipment(double w, double h, double d, double m, double c) {super(w, h, d, m); // call superclass constructor
cost = c;}
Shipment() {super();cost = -1;
}
7/28/2019 vitm java
9/23
Shipment(double len, double m, double c) {super(len, m);cost = c;
}}
class inheritance{public static void main(String args[]){Shipment shipment1 = new Shipment(10, 20, 15, 10, 3.41);Shipment shipment2 = new Shipment(2, 3, 4, 0.76, 1.28);
double vol;
vol = shipment1.volume();System.out.println("Volume of shipment1 is " + vol);System.out.println("Weight of shipment1 is " + shipment1.weight);System.out.println("Shipping cost: $" + shipment1.cost);System.out.println();
vol = shipment2.volume();System.out.println("Volume of shipment2 is " + vol);System.out.println("Weight of shipment2 is " + shipment2.weight);System.out.println("Shipping cost: $" + shipment2.cost);
}}
7/28/2019 vitm java
10/23
OUTPUT
7/28/2019 vitm java
11/23
Q. Write A Program To Show Polymorphism.
import java.io.*;class Figure{double dim1;
double dim2;
Figure(double a, double b){dim1 = a;dim2 = b;}
double area(){System.out.println("Area for Figure is undefined.");return 0;
}}
class Rectangle extends Figure{Rectangle(double a, double b){super(a, b);
}
// override area for rectangledouble area(){System.out.println("Inside Area for Rectangle.");
return dim1 * dim2;}
}
class Triangle extends Figure{Triangle(double a, double b)
7/28/2019 vitm java
12/23
{super(a, b);
}
// override area for right triangledouble area(){System.out.println("Inside Area for Triangle.");return dim1 * dim2 / 2;
}}
class areas{
public static void main(String args[]){Figure f = new Figure(10, 10);Rectangle r = new Rectangle(9, 5);Triangle t = new Triangle(10, 8);
Figure figuref;
figuref = r;System.out.println("Area is " + figuref.area());
figuref = t;System.out.println("Area is " + figuref.area());
figuref = f;System.out.println("Area is " + figuref.area());
}}
7/28/2019 vitm java
13/23
OUTPUT
7/28/2019 vitm java
14/23
Q. Write a Program to Show Use of Constructors.
import java.io.*;class Meal{Meal(){System.out.println("Meal()");
}}
class Bread{Bread()
{System.out.println("Bread()");
}}
class Cheese{Cheese(){System.out.println("Cheese()");
}}
class Lettuce{Lettuce(){System.out.println("Lettuce()");
}
}
class Lunch extends Meal{Lunch(){System.out.println("Lunch()");
7/28/2019 vitm java
15/23
}}
class PortableLunch extends Lunch{PortableLunch(){System.out.println("PortableLunch()");
}}
class Sandwich extends PortableLunch{private Bread b = new Bread();
private Cheese c = new Cheese();
private Lettuce l = new Lettuce();
public Sandwich(){System.out.println("Sandwich()");
}}
public class constructor{public static void main(String[] args){new Sandwich();
}
7/28/2019 vitm java
16/23
OUTPUT
7/28/2019 vitm java
17/23
Q. Write a program to add a class to a package
package pack; //package named pack
public class addition{private double d1,d2;public addition(double a,double b){d1=a;d2=b;}public void sum(){System.out.println("Sum="+(d1+d2));}}
Example:import pack.addition;class use{public static void main(String args[]){addition obj=new addition(10,15.5);
obj.sum();}}
7/28/2019 vitm java
18/23
OUTPUT:
7/28/2019 vitm java
19/23
Q. Write a program to add two numbers.
class sum
{public static void main(String args[]){int A, B, C;A=10;B=20;C= A+B;System.out.print(C);
}}
OUTPUT
7/28/2019 vitm java
20/23
Q Write a program to implement interface.
class interface area{
void show();void getvalue(float r);}
class square implements area{
float a;float r;
public void getvalue(float r){
a=r*r;}
public void show(){System.out.println(a);}
}class circle implements area{float pi=3.14f;
float radius,a;public void getvalue(float r){
radius=r;a=4f*pi*radius*radius;
}public void show()
{System.out.println("Radius:"+radius+" Area:"+a);
}}
7/28/2019 vitm java
21/23
class volume implements area{
float pi=3.14f;private float radius,v;
public void getvalue(float r){radius=r;v=(4f/3f)*pi*radius*radius*radius;
}public void show(){
System.out.println("Radius:"+radius+"Volume:"+v);
}}
Class inter{
public static void main(String arg[]){ volume V=new volume();circle A=new circle();square S=new square();
S.getvalue(5f);S.show();
A.getvalue(7.2f);A.show();V.getvalue(7.2f);V.show();
}}
7/28/2019 vitm java
22/23
Output:-
7/28/2019 vitm java
23/23
INDEX
S.NO. EXPERIMENTS DATE REMARK
1. Write a program to printHello Java
2. Write a program to showconcept of class in Java
3. Write a program toexception handling in
Java4. Write a program to show
Inheritance
5. Write a program to Showpolymorphism
6. Write a program to showuse of constructor
7. Write a program to add aclass to a packages
8. Write a program to add atwo no.
9. Write a program toimplement interface