Вы находитесь на странице: 1из 5

[] J A V A

JavaLab_8

LAB

CLASS and OBJECT

%Lab8_1:

A program that use the Box class

import java.io.* ;
class Box {
double width;
double height , depth;
}
public class BoxDemo
{
public static void main (String[] args) {
Box MyBox = new Box();
double Volume;
// assign values to MyBox's variables
MyBox.width = 10;
MyBox.height = 20 ;
MyBox.depth = 15 ;
// Compute the volume
Volume = MyBox.width *MyBox.height * MyBox.depth;
System.out.println("Volume is " + Volume);
} }

%Lab8_2:

Adding a method to the class Box


import java.io.* ;
class Box {
double W, H, D;
void Volume() {
System.out.print(" Volume is ");
System.out.println(W * H * D);
}
}
public class Class1{
public static void main (String[] args) {
Box mybox1 = new Box();
Box mybox2 = new Box();
mybox1.W = 10;
mybox1.H = 10;
mybox1.D = 10;

------------------------------------------------------------------------------------------------------------------------------------------------Dr Mohammed Fadhl

[] J A V A

LAB

mybox2.W = 2;
mybox2.H = 3;
mybox2.D = 4;
//Display the volume of the first box
mybox1.Volume();
//Display the volume of the second box
mybox2.Volume();
} }

%Lab8_3:

Return a value from the class Box

import java.io.* ;
class Box {
double W, H, D;
double Volume(){
return (W * H * D);
}
}
public class Class1{
public static void main (String[] args) {
Box mybox1 = new Box();
Box mybox2 = new Box();
double Vol ;
mybox1.W = 10;
mybox1.H = 10;
mybox1.D = 10;
mybox2.W = 2;
mybox2.H = 3;
mybox2.D = 4;
//Display the volume of the first box
Vol = mybox1.Volume();
System.out.println(" Volume of the first is " + Vol);
//Display the volume of the second box
Vol = mybox2.Volume();
System.out.println(" Volume of the second is " + Vol);
}
}
------------------------------------------------------------------------------------------------------------------------------------------------Dr Mohammed Fadhl

[] J A V A

%Lab8_4:

LAB

Adding a method that takes parameters

import java.io.* ;
class Box {
double W, H, D;
double Volume() {
return (W * H * D);
}
void SetDim(double a, double b, double c) {
W = a;
H = b;
D = c;
}
}
public class Class1{
public static void main (String[] args) {
Box mybox = new Box();
double Vol ;
// Initialized the variables
mybox.SetDim(3,4,5);
//Display the volume of the first box
Vol = mybox.Volume();
System.out.println(" Volume of the first is " + Vol);
} }

%Lab8_5:

Adding a constructor Box

import java.io.* ;
class Box {
double W, H, D;
Box() {
System.out.println("Constructing Box");
W = 2;
H = 3;
D = 4;
}
double Volume() {
return (W * H * D);
}
}
------------------------------------------------------------------------------------------------------------------------------------------------Dr Mohammed Fadhl

[] J A V A

LAB

public class Class1{


public static void main (String[] args) {
Box mybox = new Box();
double Vol ;
//Display the volume of the first box
Vol = mybox.Volume();
System.out.println(" Volume of the first is " + Vol);
}
}

%Lab8_6:

Adding a parameterize constructor

import java.io.* ;
class Box {
double W, H, D;
Box(double a, double b, double c) {
System.out.println("Constructing Box");
W = a;
H = b;
D = c;
}
double Volume()
{
return (W * H * D);
}
}
public class Class1{
public static void main (String[] args) {
Box mybox1 = new Box(4,4,4);
Box mybox2 = new Box(10,10,10);
double Vol ;
Vol = mybox1.Volume();
System.out.println(" Volume of the first is " + Vol);
Vol = mybox2.Volume();
System.out.println(" Volume of the second is " + Vol);
}
}
------------------------------------------------------------------------------------------------------------------------------------------------Dr Mohammed Fadhl

[] J A V A

%Lab8_7:

LAB

Define a static variable

class box {

static int count;


int J ;
box() {
J = count;
count++;
}
}
public class MY {
public static void main(String[] args) {
int i=10, j;
box p[] = new box[i];
for(j=0; j< I ; j++)
p[j] = new box();
System.out.println(" J

" + count);

for(j=0; j< i ; j++)


System.out.println( p[j].j + " " + p[j].count );
}
}

------------------------------------------------------------------------------------------------------------------------------------------------Dr Mohammed Fadhl

Вам также может понравиться