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

 

import javax.swing.*;

public class book0

{ public String name;

}

 

import javax.swing.*;

public class book0test

{

  public static void main(String arg[])

  { book0 b=new book0();

    b.name="Alice in Wonderland";

    JOptionPane.showMessageDialog(null,b.name);

  }

}

 

import javax.swing.*;

public class book1

{

  public void display(String s)

  {JOptionPane.showMessageDialog(null,"Book name : "+s+"\n","Book library",JOptionPane.PLAIN_MESSAGE);}

}

 

public class book1test

{

  public static void main(String arg[])

  {book1 b=new book1();

    b.display("Java programming Language");

  }

}

 

 

import javax.swing.*;

public class book2

{ String name;

  public void setName(String s)

  {name=s;}

  public String getName()

  {return name;}

  public void display()

  {JOptionPane.showMessageDialog(null,"Book name : "+name+"\n","Book library",JOptionPane.PLAIN_MESSAGE);}

}

 

import javax.swing.*;

public class book2test

{

  public static void main(String arg[])

  { book2 b2=new book2();

    b2.setName("Alice in Wonderland");

    String s=b2.getName();

    JOptionPane.showMessageDialog(null,"Book name (inside main) = "+s);

    b2.display();

  }

}

 

 

 

Constructor method

import javax.swing.*;

public class book3

{ String name;

  public book3(String s)

  {name=s;}

  public void setName(String s)

  {name=s;}

  public String getName()

  {return name;}

  public void display()

  {JOptionPane.showMessageDialog(null,"Book name : "+name+"\n","Book library",JOptionPane.PLAIN_MESSAGE);}

}

 

import javax.swing.*;

public class book3test

{

  public static void main(String arg[])

  { book3 b3=new book3("Alice in Wonderland");

    b3.display();

  }

}

 

 

 

 

import javax.swing.*;

public class book4

{ String name;

  public book4(String s)

  {name=s;}

  public String getName()

  {return name;}

  public String toString()

  {String s1="Book name : "+name+"\n";return s1;}

  public static void display(String s2)

{JOptionPane.showMessageDialog(null,"Book name : "+s2+"\n","Book library",JOptionPane.PLAIN_MESSAGE);}

}

 

import javax.swing.*;

public class book4test

{

  public static void main(String arg[])

  { book4 b4=new book4("Alice in Wonderland");

    String s=b4.toString();

    book4 b5=new book4("Java Programming Language");

    s+=b5.toString();

    book4.display(s);

  }

}

 

 

 

 

import javax.swing.*;

public class book5

{ String name,author;

  public book5(String namei,String authori)

  {name=namei;author=authori;}

  public void setName(String s)

  {name=s;}

  public String getName()

  {return name;}

  public void setAuthor(String s)

  {name=s;}

  public String getAuthor()

  {return author;}

  public String toString()

  {String s1="Book name : "+name+"\nAuthor . "+author+"\n";return s1;}

  public static void display(String s2)

  {JOptionPane.showMessageDialog(null,"Book name : "+s2+"\n","Book library",JOptionPane.PLAIN_MESSAGE);}

}

 

import javax.swing.*;

public class book5test1

{

  public static void main(String arg[])

  { book5 b4=new book5("Alice in Wonderland","Lewis Carol");

    String s=b4.toString();

    book5 b5=new book5("Java Programming Language","M. Turhan Coban");

    s+=b5.toString();

    JOptionPane.showMessageDialog(null,s);

  }

}

 

 

 

GROUP 2 ACCOUNT CLASSES

 

public class Account

{

private double balance; // instance variable that stores the balance

// constructor

 public Account(double initialBalance )

{

// validate that initialBalance is greater than 0.0;

// if it is not, balance is initialized to the default value 0.0

if ( initialBalance > 0.0 )

balance = initialBalance;

} // end Account constructor

 

// credit (add) an amount to the account

public void credit(double amount )

{

balance = balance + amount; // add amount to balance

} // end method credit

// draft (substract) an amount from the account

public void draft(double amount )

{

balance = balance - amount;

if(balance<0) balance=0;// substract amount from balance

} // end method draft

// return the account balance

public double getBalance()

{

return balance; // gives the value of balance to the calling method

} // end method getBalance

} // end class Account

 

import javax.swing.*;

public class AccountTest

{

public static void main(String arg[])

{double amount1=Double.parseDouble(JOptionPane.showInputDialog("amount1 =?"));

 double amount2=Double.parseDouble(JOptionPane.showInputDialog("amount2 =?"));

 Account a1=new Account(amount1);

 Account a2=new Account(amount2);

 double amount3=Double.parseDouble(JOptionPane.showInputDialog("amount3 =?"));

 a1.credit(amount3 );

 a2.draft(amount3 );

 String s1="Bank acounts : \n";

 s1+="acount 1  balance = "+a1.getBalance()+"\n";

 s1+="acount 2  balance = "+a2.getBalance()+"\n";

 JOptionPane.showMessageDialog(null,s1);

}

}

 

 

 

import javax.swing.*;

public class Account1

{

private String name;

private int accountno;

private double balance; // instance variable that stores the balance

// constructor

 public Account1(String namei,double initialBalance )

{

// validate that initialBalance is greater than 0.0;

// if it is not, balance is initialized to the default value 0.0

name=namei;

accountno=(int)(1000*Math.random());

if ( initialBalance > 0.0 )

balance = initialBalance;

String s1="Welcome "+name+" please record your account no to acess your account : \n";

 s1+="acount no = "+accountno+"\n";

 s1+="name = "+name+"\n";

 s1+="name = "+balance+"\n";

 JOptionPane.showMessageDialog(null,s1);

} // end Account constructor

 

// credit (add) an amount to the account

public void accountnumber()

{int accountnoi=0;

while(accountno!=accountnoi)

   {accountnoi=Integer.parseInt(JOptionPane.showInputDialog("Welcome "+name+"please enter your account number =?"));}

}

public void credit(double amount )

{

accountnumber();

balance = balance + amount; // add amount to balance

} // end method credit

// draft (substract) an amount from the account

public void draft(double amount )

{

accountnumber();

balance = balance - amount;

if(balance<0) balance=0;// substract amount from balance

} // end method draft

// return the account balance

public double getBalance()

{

return balance; // gives the value of balance to the calling method

} // end method getBalance

} // end class Account

 

import javax.swing.*;

public class AccountTest1

{

public static void main(String arg[])

{double amount1=Double.parseDouble(JOptionPane.showInputDialog("amount1 =?"));

 Account1 a1=new Account1("Turhan",amount1);

 double amount3=Double.parseDouble(JOptionPane.showInputDialog("amount3 =?"));

 a1.credit(amount3 );

 String s1="Bank acounts : \n";

 s1+="acount 1  balance = "+a1.getBalance()+"\n";

 JOptionPane.showMessageDialog(null,s1);

}

}

 

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