Java Lab 4

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Omar Mukhtar University Faculty of Engineering

Dept. of Computer Engineering


Albeida , Libya
Instructor: Mrs. Krishna Kumari Ganga
Advanced Programming 1: Lab sheet 4

Class and Object


1. Program to find the volume of a box by using method.
class Box {
double width;
double height;
double depth;
double volume() {
return width * height * depth;
}
public static void main(String args[]) {
BoxE mybox1 = new BoxE();
double vol;
vol = mybox1.volume();
System.out.println("Volume is " + vol);
}
}

2. Program to find the volume of a box by using a parameterized method.


class Box {
double width;
double height;
double depth;
double volume() {
return width * height * depth;
}
void setDim(double w, double h, double d) {
width = w;
height = h;
depth = d;
}
}

class BoxVolume {
public static void main(String args[]) {
Box mybox1 = new Box();
Box mybox2 = new Box();
double vol;
mybox1.setDim(10, 20, 15);
mybox2.setDim(3, 6, 9);
vol = mybox1.volume();
System.out.println("Volume is " + vol);
vol = mybox2.volume();
System.out.println("Volume is " + vol);
}
}

3. Program using Method overriding


class A1 {
int i, j;
A1(int a, int b) {
i = a; j = b;
}
void show() {

Advanced Programming Using Java - Prepared by: Krishna Kumari Ganga


System.out.println("i and j: " + i + " " + j);
}
}
class B1 extends A1 {
int k;
B1(int a, int b, int c) {
super(a, b);
k = c;
}
void show() {
super.show();
System.out.println("k: " + k);
}
}

class Overriding1 {
public static void main(String args[]) {
B1 subOb = new B1(1, 2, 3);
subOb.show();
}
}

4. Program to perform simple operations on the given two numbers using Switch Statement
import java.io.DataInputStream;
import java.io.IOException;
class Calc {
public static void main(String args[]) throws IOException {
DataInputStream pt=new DataInputStream(System.in);
int x,y,z,ch;
System.out.println("Enter Value of X:");
x = Integer.parseInt(pt.readLine());
System.out.println("Enter Value of Y:");
y = Integer.parseInt(pt.readLine());
System.out.println("1.Addition\n2.Subtraction \n3.Multiplication\n4.Division");
System.out.println("Enter ur choice:");
ch = Integer.parseInt(pt.readLine());
switch(ch) {
case 1:
z = x + y;
System.out.println("The Addition is:"+z);
break;
case 2:
z = x - y;
System.out.println("The Subtraction is:"+z);
break;

case 3:
z = x * y;
System.out.println("The Multiplication is:"+z);
break;
case 4:
z = x / y;
System.out.println("The Division is:"+z);
break;
default:
System.out.println("Sorry Try Again.........");
break;

Advanced Programming Using Java - Prepared by: Krishna Kumari Ganga


}
}
}

5. Simple program using this keyword for variable hiding


class Thistest {
int x=10;
public static void main(String args[]) {
Thistest t= new Thistest ();
t.display();
t.display(15);
}
void display(int x) {
x=5;
System.out.println("Value of the instance variable is " + this.x);
System.out.println("Value of the local variable is " + x);
}
void display() {
int x=25;
System.out.println("Value of the instance variable is " + this.x);
System.out.println("Value of the local variable is " + x);
}
}

6. Program to find the greatest of 3 numbers using this


class Construct {
int a,b, c;
Construct( int a) {
this.a=a;
}
Construct(int a, int b,int c) {
this(a);
this.b=b;
this.c=c;
}
int great() {
if(a>b &&a>c)
return a;
else if(b>c)
return b;
else return c;
}
void display() {
System.out.println("Gretest no:"+great());
}
}
class Greatest_Main {
public static void main(String ar[]) {
Construct C1 =new Construct( 2, 4 ,8);
C1.display();
}
}

7. Program to display the student details by using Constructor overloading


class Student {
private int stuID;
private String stuName;

Advanced Programming Using Java - Prepared by: Krishna Kumari Ganga


private int stuAge;
Student() {
stuID = 100;
stuName = "New Student";
stuAge = 18;
}
Student(int num1, String str, int num2) {
stuID = num1;
stuName = str;
stuAge = num2;
}
//Getter and setter methods
public int getStuID() {
return stuID;
}
public void setStuID(int stuID) {
this.stuID = stuID;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public int getStuAge() {
return stuAge;
}
public void setStuAge(int stuAge) {
this.stuAge = stuAge;
}
}

public class StudentData {


public static void main(String args[]) {
Student S1 = new Student();
System.out.println("Student Name is: "+S1.getStuName());
System.out.println("Student Age is: "+S1.getStuAge());
System.out.println("Student ID is: "+S1.getStuID());

Student S2 = new Student(110, "Maryum", 16);


System.out.println("Student Name is: "+S2.getStuName());
System.out.println("Student Age is: "+S2.getStuAge());
System.out.println("Student ID is: "+S2.getStuID());

Student S3 = new Student(111, "Radwan", 21);


System.out.println("Student Name is: "+S3.getStuName());
System.out.println("Student Age is: "+S3.getStuAge());
System.out.println("Student ID is: "+S3.getStuID());

Student S4 = new Student(112, "Saleh", 21);


System.out.println("Student Name is: "+S4.getStuName());
System.out.println("Student Age is: "+S4.getStuAge());
System.out.println("Student ID is: "+S4.getStuID());

Student S5 = new Student(113, "Mohammed", 22);


System.out.println("Student Name is: "+S5.getStuName());
System.out.println("Student Age is: "+S5.getStuAge());

Advanced Programming Using Java - Prepared by: Krishna Kumari Ganga


System.out.println("Student ID is: "+S5.getStuID());
}
}

8. Program to Add and Subtract Numbers using Class and Inheritance


class Pqr {
int a, b;
void getdata() {
a=10;
b=20;
System.out.println("The value of a = " + a);
System.out.print("The value of b = " + b);
}
}

class Sum extends Pqr {


int sum;
void sum() {
sum = a + b;
System.out.print("\nSum = " + sum);
}
}

class Subt extends Sum {


int subt;
void subtract() {
subt = a - b;
System.out.print("\nSubtraction = " + subt + "\n");
}
}

class Abc {
public static void main(String args[]) {
Subt obj = new Subt();
obj.getdata();
obj.sum();
obj.subtract();
}
}

9. Program to display volume and weight of the box using inheritance


class Box {
double width;
double height;
double depth;
Box(double w, double h, double d) {
width = w; height = h; depth = d;
}
Box() {
width = -1;
height = -1;
depth = -1;
}
Box(double len) {
width = height = depth = len;
}
double volume() {

Advanced Programming Using Java - Prepared by: Krishna Kumari Ganga


return width * height * depth;
}
}
//Here, Box is extended to include weight.

class BoxWeight extends Box {


double weight;
BoxWeight(double w, double h, double d, double m) {
width = w;
height = h;
depth = d;
weight = m;
}
}

class Inhert {
public static void main(String args[]) {
BoxWeight mybox1 = new BoxWeight(10, 20, 15, 34.3);
BoxWeight mybox2 = new BoxWeight(2, 3, 4, 0.076);
Box b3=new Box();
Box b4=new Box(5);
double vol;
vol = mybox1.volume();

System.out.println("Volume of mybox1 is " + vol);


System.out.println("Weight of mybox1 is " + mybox1.weight);
System.out.println();
vol = mybox2.volume();
System.out.println("Volume of mybox2 is " + vol);
System.out.println("Weight of mybox2 is " + mybox2.weight);
System.out.println();
vol = b3.volume();
System.out.println("Volume of b3 is " + vol);
System.out.println();
vol = b4.volume();
System.out.println("Volume of b4 is " + vol);
}
}

10. Example program using super


//Using super to overcome name hiding.
class A {
int i;
}

class B extends A {
int i;
// this i hides the i in A
B(int a, int b) {
super.i = a;
// i in A
i = b;
// i in B
}
void show() {
System.out.println("i in superclass: " + super.i);
System.out.println("i in subclass: " + i);

Advanced Programming Using Java - Prepared by: Krishna Kumari Ganga


}
}

class SuperEx {
public static void main(String args[]) {
B subOb = new B(1, 2);
subOb.show();
}
}

11. Program to display volume and weight of the box using inheritance and super
class Box1 {
private double width;
private double height;
private double depth;
Box1(double w, double h, double d) {
width = w;
height = h;
depth = d;
}
Box1() {
width = -1;
height = -1;
depth = -1;
}
Box1(double len) {
width = height = depth = len;
}
double volume() {
return width * height * depth;
}
}

class BoxWeight1 extends Box1 {


double weight;
BoxWeight1(double w, double h, double d, double m) {
super(w, h, d);
weight = m;
}
BoxWeight1() {
super();
weight = -1;
}
BoxWeight1(double len, double m) {
super(len);
weight = m;
}
}
class Inher2 {
public static void main(String args[]) {
BoxWeight1 mybox1 = new BoxWeight1(10, 20, 15, 34.3);
BoxWeight1 mybox2 = new BoxWeight1(2, 3, 4, 0.076);
BoxWeight1 mybox3 = new BoxWeight1();
BoxWeight1 mycube = new BoxWeight1(3, 2);
double vol;
vol = mybox1.volume();
System.out.println("Volume of mybox1 is " + vol);

Advanced Programming Using Java - Prepared by: Krishna Kumari Ganga


System.out.println("Weight of mybox1 is " + mybox1.weight);
System.out.println();
vol = mybox2.volume();
System.out.println("Volume of mybox2 is " + vol);
System.out.println("Weight of mybox2 is " + mybox2.weight);
System.out.println();
vol = mybox3.volume();
System.out.println("Volume of mybox3 is " + vol);
System.out.println("Weight of mybox3 is " + mybox3.weight);
System.out.println();
vol = mycube.volume();
System.out.println("Volume of mycube is " + vol);
System.out.println("Weight of mycube is " + mycube.weight);
System.out.println();
}
}

12. Program to display Simple Bank Transaction


import java.io.*;
class Acc {
static float bal=0.0f;
void dep(float a) {
bal=bal+a;
balance();
}
void wdraw(float a) {
if(a<=bal)
bal=bal-a;
else
System.out.println("Not enough balance in your account");
balance();
}
void balance() {
System.out.println("The current balance in your account is: "+bal);
}
}
class MainAcc {
public static void main(String arg[]) throws IOException {
int ch=0,z=0;
float a=0.0f;
Acc a1=new Acc();
DataInputStream ins=new DataInputStream(System.in);
System.out.println("1:Deposit");
System.out.println("2:Withdraw");
do {
System.out.println("Enter ur choice ");
ch=Integer.parseInt(ins.readLine());
if(ch==1) {
System.out.print("Enter how much money u want to deposit ");
a=Integer.parseInt(ins.readLine());
a1.dep(a);
}
else if(ch==2) {
System.out.print("Enter how much money u want to withdraw ");
a=Integer.parseInt(ins.readLine());
a1.wdraw(a);
} else

Advanced Programming Using Java - Prepared by: Krishna Kumari Ganga


System.out.println("Not a valid choice");
System.out.println("Press 0 if u want another transaction ");
z=Integer.parseInt(ins.readLine());
}
while(z==0);
}
}

13. Program to Implement this and super Keyword


class Vehicle {
int id;
String color;
Vehicle(int regno,String color) {
id = regno;
this.color = color;
}
}
class Vchl extends Vehicle {
int id;
String color;
Vchl(int i1,int i2,String s1,String s2) {
super(i2,s2);
id = i1;
color = s1;
}
void pdata() {
System.out.print(super.id);
}
void ddata() {
System.out.print(super.color);
}
public static void main(String arg[]) {
Vchl v = new Vchl(10,2334,"GREY","WHITE");
System.out.print("Vehicle color is ");
v.ddata();
System.out.print("\nVehicle number is ");
v.pdata();
System.out.println("\nVehicle engine color is " + v.color);
System.out.println("Vehicle engine id is " + v.id);
}
}

14. Program using abstract class


abstract class Addition {
abstract void add(int a,int b);
abstract void add(int a,int b,int c);
}
class Addition1 extends Addition {
void add(int a,int b) {
int result;
result=a+b;
System.out.println("Addition of 2 numbers="+result);
}
void add(int a,int b,int c) {
int result;
result=a+b+c;

Advanced Programming Using Java - Prepared by: Krishna Kumari Ganga


System.out.println("Addition of 3 numbers="+result);
}
}

class AbstractEx {
public static void main(String args[]) {
Addition1 a1=new Addition1();
a1.add(10,20);
a1.add(4,7,9);
}
}

15. Programming to display the area of a Rectangle and triangle using Abstract class
abstract class Figure {
double dim1;
double dim2;
Figure(double a, double b) {
dim1 = a;
dim2 = b;
}
// area is now an abstract method
abstract double area();
}
class Rectangle extends Figure {
Rectangle(double a, double b) {
super(a, b);
}
// override area for rectangle
double area() {
System.out.println("Inside Area for Rectangle.");
return dim1 * dim2;
}
}
class Triangle extends Figure {
Triangle(double a, double b) {
super(a, b);
}
// override area for right triangle
double area() {
System.out.println("Inside Area for Triangle.");
return dim1 * dim2 / 2;
}
}
class AbstractArea{
public static void main(String args[]) {
Rectangle r = new Rectangle(9, 5);
Triangle t = new Triangle(10, 8);
Figure fig; // this is OK, no object is created
fig = r;
System.out.println("Area is " + fig.area());
fig = t;
System.out.println("Area is " + fig.area());
}
}

Best of luck

Advanced Programming Using Java - Prepared by: Krishna Kumari Ganga

You might also like