COMPUTER PROGRAMMING I 2013 WEEK 5
DYNAMIC
METHODS AND CLASS AND OBJECT
EXERCISES
(EXERCISE problems are for your understanding the
topic. Will not return as homework)
GROUP 1 BOOK CLASSES
|
|
|
Constructor
method
|
|
|
GROUP 2 ACCOUNT CLASSES
|
|
GROUP 3 BOX CLASSES
class box { double length; double width; double height; } |
import javax.swing.*; class boxtest { public static void main(String args[]) { double volume; String s=""; box boxnumber1= new box(); boxnumber1.length=1.0; boxnumber1.width=1.2; boxnumber1.height=0.4;
volume=boxnumber1.length*boxnumber1.width*boxnumber1.height; s=s+"volume
of the box : "+volume+"\n"; JOptionPane.showMessageDialog(null,s, "class example:boxtest",JOptionPane.PLAIN_MESSAGE); System.exit(0);} } |
class box1 { double length; double width; double height; //constructor
class box1(double
l,double w, double h) {length=l; width=w; height=h; } } |
import javax.swing.JOptionPane; class boxtest1 { public static void main(String args[]) { double volume; String s=""; box1
boxnumber2= new box1(1.0,1.2,0.4);
volume=boxnumber2.length*boxnumber2.width*boxnumber2.height; s=s+"volume
of the box : "+volume+"\n"; JOptionPane.showMessageDialog(null,s, "class example:boxtest1",JOptionPane.PLAIN_MESSAGE); System.exit(0);} } |
class box2 { double length; double width; double height; box2(double l,double w, double h) {length=l; width=w; height=h; } double volume() {return length*width*height;} } |
import javax.swing.JOptionPane; class boxtest2 { public static void main(String args[]) { double volume; String s=""; box2 boxnumber3= new box2(1.0,1.2,0.4); volume=boxnumber3.volume(); s=s+"volume
of the box : "+volume+"\n"; JOptionPane.showMessageDialog(null,s,"class
example:boxtest2",JOptionPane.PLAIN_MESSAGE); } } |
class box3 { double
length; double
width; double
height; box3(double
l,double w, double h) {length=l; width=w; height=h; } double
volume() {return
length*width*height;} public
String toString() { String s=""; s+="length =
"+length+"\n"; s+="width =
"+width+"\n"; s+="height =
"+height+"\n"; s+="Volume =
"+volume()+"\n"; return s; } } |
import javax.swing.JOptionPane; class boxtest3 { public static void main(String args[]) { double volume; String s=""; box3 boxnumber4= new box3(1.0,1.2,0.4); JOptionPane.showMessageDialog(null,boxnumber4.toString(),
"class example:boxtest3",JOptionPane.PLAIN_MESSAGE); System.exit(0);} } |
class box4 { double length; double width; double height; box4(double
l,double w, double h) {length=l; width=w; height=h; } box4(box4
bi) {length=bi.length; width=bi.width; height=bi.height; } double
volume() {return
length*width*height;} public
String toString() { String s=""; s+="length =
"+length+"\n"; s+="width =
"+width+"\n"; s+="height =
"+height+"\n"; s+="Volume =
"+volume()+"\n"; return s; } } |
import javax.swing.JOptionPane; class boxtest4 { public static void main(String args[]) { double volume; box4 firstbox= new box4(1.0,1.2,0.4); box4 secondbox= new box4(firstbox); String s="First box : \n"+firstbox.toString()+"\n"; s+="Second box : \n"+secondbox.toString()+"\n"; JOptionPane.showMessageDialog(null,s, "class example:boxtest4",JOptionPane.PLAIN_MESSAGE); System.exit(0);} } |
GROUP4 ROBOT CLASS
public class
robot { public String name; public double R; public double theta; public robot(String is)
{R=0;theta=0;name=is;} public robot(String is,double
Ri) {name=is;R=Ri;theta=0;} public robot(String is,double
Ri,double theta_angle) {name=is;R=Ri;theta=theta_angle*Math.PI/180.0;} public robot(robot r1) {name=r1.name;R=r1.R;theta=r1.theta;} public void
turn_north() {theta=Math.PI/2.0;} public void
turn_south() {theta=3.0*Math.PI/2.0;} public void
turn_west() {theta=Math.PI;} public void
turn_east() {theta=Math.PI;} public void
turn(double angle) {theta+=angle*Math.PI/180.0;} public void forward() {R+=1;} public void forward(double Ri) {R+=Ri;} public void backward() {R-=1;} public void backward(double Ri) {R-=Ri;} public String toString() { String
s="------------"+name+"--------------------\n"; s+="cartesian coordinates x =
"+R*Math.sin(theta)+" y =
"+R*Math.cos(theta)+" \n"; s+="polar coordinates R =
"+R+" theta = "+theta*180/Math.PI+"
\n";return s; } } |
import javax.swing.*; public class
robottest { public static void main(String args[]) {robot R2D2=new
robot("aRtuDitu"); robot z2=new
robot("ali",1.0,90.0); robot z3=new
robot(R2D2); String s="";
R2D2.turn_north();R2D2.forward();s+=R2D2.toString(); R2D2.turn_west();R2D2.forward(2.0);s+=R2D2.toString(); z2.turn_east();z2.forward(3.0);s+=z2.toString();
z2.turn_south();z2.forward();s+=z2.toString(); s+="copy
robot : "+z3.toString(); JOptionPane.showMessageDialog(null,s,
"robot class test",JOptionPane.PLAIN_MESSAGE); } } |
GROUP 5
COMPLEX CLASSES
public class
complex1 { public double real; public double imaginary; } |
import javax.swing.*; public class
complex1test { public static void main(String args[]) {complex1 z1=new complex1(); z1.real=2.0; z1.imaginary=3.25; String s="complex number =
"+z1.real+" + i*"+z1.imaginary+" \n"; complex1 z2=new complex1(); z2.real=1.1; z2.imaginary=2.0; s+="complex
number = "+z2.real+" + i*"+z2.imaginary+" \n";
complex1 z3=new complex1(); z3.real=z1.real+z2.real; z3.imaginary=z1.imaginary+z2.imaginary; s+="total
of complex number = "+z3.real+" + i*"+z3.imaginary+"
\n"; JOptionPane.showMessageDialog(null,s,
"complex1 sınıfı testi",JOptionPane.PLAIN_MESSAGE); } } |
public class complex3 { public double real; public double imaginary; public complex3(double g,double s) {real=g; imaginary=s; } public complex3(complex3 s1) {real=s1.real; imaginary=s1.imaginary; } public void add(complex3 s1) {real+=s1.real; imaginary+=s1.imaginary; } public static complex3 add(complex3
s1,complex3 s2) {complex3 z3=new
complex3((s1.real+s2.real),(s1.imaginary+s2.imaginary)); return z3; } public String toString() { String
s=""; if(imaginary>0) {s+=real+" +
i*"+imaginary+" \n";} else
if(imaginary<0) {s+=real+" - i*"+(-imaginary)+" \n";} else
{s+=real+" \n";} return s; } } |
import javax.swing.*; public class
complex3test { public static void main(String args[]) { complex3 z1=new
complex3(2.0,3.5); complex3 z2=new complex3(1.1,2.0); complex3 z3=new complex3(z2); z3.add(z1); String s="z1 =
"+z1.toString()+"z2 = "+z3.toString()+"total z3 =
"+z3.toString(); JOptionPane.showMessageDialog(null,s,
"complex3 sınıfı testi",JOptionPane.PLAIN_MESSAGE); } } |
import javax.swing.*; public class
complex2test { public static void main(String args[]) {String sa=JOptionPane.showInputDialog("a="); String sb=JOptionPane.showInputDialog("b="); String sc=JOptionPane.showInputDialog("c="); double a=Double.parseDouble(sa); double b=Double.parseDouble(sb); double c=Double.parseDouble(sc); double x1r=0; double x1i=0; double x2r=0; double x2i=0; double det=b*b-4.0*a*c; if(det>0) {x1r=(-b+Math.sqrt(det))/(2.0*a);x2r=(-b-Math.sqrt(det))/(2.0*a);} else if(det==0)
{x1r=x2r=-b/(2.0*a);} else {x1r=x2r=-b/(2.0*a);x1i=Math.sqrt(-det)/(2.0*a);x2i=-Math.sqrt(-det)/(2.0*a);} complex3 r1=new complex3(x1r,x1i); complex3 r2=new complex3(x2r,x2i); String s=new String("root 1 =
"+r1.toString()+"root 2 = "+r2.toString()); JOptionPane.showMessageDialog(null,s,
"roots of quadratic polynomial",JOptionPane.PLAIN_MESSAGE); } } |
HOMEWORKS (You will return
solutions of this sets as homework)
HW1
A point in two
dimensional cartesian coordinate system is shown as x and y. Write a class to
represent the point with output method to show the coordinates of the point
HW
2 Add private String
variable color of the box to box5 class and add up methods to Access the color
and to change the color to to the class
public String
read_color() {…….}
public void write_color(String
col) {…….}
HW
3 Multiplication
of two complex number z1=Re1+i*Im1 and z2= z1=Re2+i*Im2 is defined as:
z1*z2=(Re1*Re2-Im1*Im2) +
i*(Re1*Im2+Re2*Im1) add two methods to complex3 class
public void multiply(complex3
s1)
public static complex3 multiply(complex3
s1,complex3 s2)
and test the methods through test
programs
HW 4:
A Force vector is
defined with force components as F=Fx* i+Fy*j where i and j are unit vectors in x
and y directions. Write a class to define a Force vector. In your definition
also add a method to add two vectors. Note : rule of
adding two vectors as F1=Fx1*i+Fy1*j and
F2=Fx2*i+Fy2*j is
F=F1+F2=(Fx1+Fx2)*i+(Fy1+Fy2)*j