COMPUTER PROGRAMMING  2014 WEEK 7

COMPOSITION, INHERITANCE; ABSTRACT CLASSES AND INTERFACE

SECTION 1 : COMPOSITION

EXERCISE 1

 public class point

{

public double x[];

public point(double xi,double yi,double zi)

{ x=new double[3];

x[0]=xi;x[1]=yi;x[2]=zi;}

 

public point(double xi[])

{ x=new double[3];

for(int i=0;i<3;i++) x[i]=xi[i];

}

public point()

{ x=new double[3];

for(int i=0;i<3;i++) x[i]=0.0;

}

public point(point pi)

{x=new double[3];

for(int i=0;i<3;i++) x[i]=pi.x[i];}

 

public double R()

{return Math.sqrt(x[0]*x[0]+x[1]*x[1]+x[2]*x[2]);}

 

public double[] costeta()

{

double r=R();

double cos[]=new double[3];

for(int i=0;i<3;i++) cos[i]=x[i]/r;

return cos;

}

public double[] teta()

{

double teta[]=new double[3];

double costeta[]=costeta();

for(int i=0;i<3;i++) teta[i]=Math.acos(costeta[i]);

return teta;

}

public double[] dx(point pi)

{ double d[]=new double[3];

  for(int i=0;i<3;i++) d[i]=pi.x[i]-x[i];

  return d;

}

public double dR(point pi)

{ double total=0;

  for(int i=0;i<3;i++)

  {total+=(x[i]-pi.x[i])*(x[i]-pi.x[i]);}

               return Math.sqrt(total);

}

public double[] dcosteta(point pi)

{

double dr=dR(pi);

double dx[]=dx(pi);

double dcos[]=new double[3];

for(int i=0;i<3;i++) dcos[i]=dx[i]/dr;

return dcos;

}

public double[] dteta(point pi)

{

double dcos[]=dcosteta(pi);

double dt[]=new double[3];

for(int i=0;i<3;i++) dt[i]=Math.acos(dcos[i]);

return dt;

}

 

public String toString()

{String s="P["+x[0]+","+x[1]+","+x[2]+"]";

 return s;

}

 

public String name(){return "point";}

}

 

 public class Force

{

public point P;

public double F[];

public double M[];

 

public Force(double x[],double Fi[],double Mi[])

{ P=new point(x);

  F=new double[3];

  M=new double[3];

  for(int i=0;i<3;i++) {F[i]=Fi[i];M[i]=Mi[i];}

}

 

public Force(double x[],double Fi[])

{ P=new point(x);

  F=new double[3];

  M=new double[3];

  for(int i=0;i<3;i++) {F[i]=Fi[i];}

}

 

public Force(double Fi[])

{ double Pi[]=new double[3];

  P=new point(Pi);

  F=new double[3];

  M=new double[3];

  for(int i=0;i<3;i++) {F[i]=Fi[i];}

}

 

public Force(double x[],double R,double costeta[])

{ P=new point(x);

  F=new double[3];

  M=new double[3];

  for(int i=0;i<3;i++) {F[i]=R*costeta[i];}

}

//two points in space is given to define force

public Force(double x0[],double x1[],double R)

{ P=new point(x0);

  point P1=new point(x1);

  double costeta[]=P.dcosteta(P1);

  F=new double[3];

  M=new double[3];

  for(int i=0;i<3;i++) {F[i]=R*costeta[i];}

}

//two points in space is given to define force

//first point 0,0,0 cartasian starting point

public Force(double x1[],double R)

{ P=new point();

  point P1=new point(x1);

  double costeta[]=P.dcosteta(P1);

  F=new double[3];

  M=new double[3];

  for(int i=0;i<3;i++) {F[i]=R*costeta[i];}

}

 

public Force(double R,double costeta[])

{ double Pi[]=new double[3];

  P=new point(Pi);

  F=new double[3];

  M=new double[3];

  for(int i=0;i<3;i++) {F[i]=R*costeta[i];}

}

public Force(double R,double costeta)

{ double Pi[]=new double[3];

  P=new point(Pi);

  F=new double[3];

  M=new double[3];

  double tet=Math.acos(costeta);

  double costeta1[]={costeta,Math.sin(tet),0.0};

  for(int i=0;i<3;i++) {F[i]=R*costeta1[i];}

}

 

public Force(Force Fi)

{ P=new point(Fi.P);

  F=new double[3];

  M=new double[3];

  for(int i=0;i<3;i++) {F[i]=Fi.F[i];M[i]=Fi.M[i];}

}

public double RF()

{return Math.sqrt(F[0]*F[0]+F[1]*F[1]+F[2]*F[2]);}

 

public double RM()

{return Math.sqrt(M[0]*M[0]+M[1]*M[1]+M[2]*M[2]);}

 

public double[] costetaF()

{

double r=RF();

double cos[]=new double[3];

for(int i=0;i<3;i++) cos[i]=F[i]/r;

return cos;

}

 

public double[] tetaF()

{

double r=RF();

double cos[]=new double[3];

for(int i=0;i<3;i++) cos[i]=Math.acos(F[i]/r);

return cos;

}

 

public double[] costetaM()

{

double r=RF();

double cos[]=new double[3];

for(int i=0;i<3;i++) cos[i]=M[i]/r;

return cos;

}

 

public double[] tetaM()

{

double r=RF();

double cos[]=new double[3];

for(int i=0;i<3;i++) cos[i]=Math.acos(M[i]/r);

return cos;

}

 

public void move(point p)

{double dp[]=P.dx(p);

 for(int i=0;i<3;i++) {P.x[i]=p.x[i];}

 M[0]+=dp[1]*F[2]-dp[2]*F[1];

 M[1]+=dp[2]*F[0]-dp[0]*F[2];

 M[2]+=dp[0]*F[1]-dp[1]*F[0];

}

 

public void movetopoint(point p,Force Fi)

{ add(Fi);

  move(p);

}

 

//Force addition

public void add(Force Fi)

{double dp[]=P.dx(Fi.P);

 M[0]+=dp[1]*F[2]-dp[2]*F[1];

 M[1]+=dp[2]*F[0]-dp[0]*F[2];

 M[2]+=dp[0]*F[1]-dp[1]*F[0];

 for(int i=0;i<3;i++){F[i]+=Fi.F[i];}

}

//3D rotation

public void turnteta(double teta1[])

{double RF=RF();

 double teta2[]=tetaF();

 double teta[]=new double[3];

 for(int i=0;i<3;i++)

  {   teta[i]=teta1[i]+teta2[i];

                 F[i]=RF*Math.cos(teta[i]);

  }

}

 

//Plane(2D) rotation

public void turnteta(double teta1)

{double RF=RF();

 double teta2[]=tetaF();

 double teta3[]={teta1,teta1,0.0};

 double teta[]=new double[3];

 for(int i=0;i<3;i++)

  {   teta[i]=teta2[i]+teta3[i];

                 F[i]=RF*Math.cos(teta[i]);

  }

}

 

//3D rotation

public void turncosteta(double costeta2[])

{double RF=RF();

 double costeta1[]=costetaF();

 double teta1[]=new double[3];

 double teta2[]=new double[3];

 double teta[]=new double[3];

 for(int i=0;i<3;i++)

  {  teta1[i]=Math.acos(costeta1[i]);

     teta2[i]=Math.acos(costeta2[i]);

     teta[i]=teta1[i]+teta2[i];

                 F[i]=RF*Math.cos(teta[i]);}

}

 

//Plane rotation

public void turncosteta(double costeta)

{double RF=RF();

 double costeta1[]=costetaF();

 double costeta2[]={costeta,costeta,1.0};

 double teta[]=new double[3];

 double teta1[]=new double[3];

 double teta2[]=new double[3];

 for(int i=0;i<3;i++)

  {  teta1[i]=Math.acos(costeta1[i]);

     teta2[i]=Math.acos(costeta2[i]);

     teta[i]=teta1[i]+teta2[i];

                 F[i]=RF*Math.cos(teta[i]);}

}

 

public String toString()

{

String s=P.toString()+"\n";

s+="F["+F[0]+","+F[1]+","+F[2]+"]\n";   

s+="M["+M[0]+","+M[1]+","+M[2]+"]";

return s;

}

public String toString(String s1,double x[])

{

String s=P.toString()+"\n";

s+=s1+"["+x[0]+","+x[1]+","+x[2]+"]\n";             

return s;

}

}

 

public class forcetest1

{

public static void main(String arg[])

{

double cosfi1[]={Math.cos(Math.PI/2.0),Math.cos(0.0),Math.cos(Math.PI/2.0)};

double F=784.0;

Force F1=new Force(F,cosfi1);

System.out.println(F1.toString());

double cosfi2[]={Math.cos(20.0*Math.PI/180.0),Math.cos(20.0*Math.PI/180.0),Math.cos(0.0)};

F1.turncosteta(cosfi2);

System.out.println(F1.toString());

}

}

 

---------- Capture Output ----------

> "c:\java\bin\javaw.exe" forcetest1

P[0.0,0.0,0.0]

F[4.800615452657625E-14,784.0,4.800615452657625E-14]

M[0.0,0.0,0.0]

P[0.0,0.0,0.0]

F[-268.1437923673241,736.7190146961522,4.800615452657625E-14]

M[0.0,0.0,0.0]

 

> Terminated with exit code 0.

 

PROBLEM:

public class forcetest2

{

public static void main(String arg[])

{

double cosfi1[]={-Math.cos(Math.PI/3.0),Math.cos(Math.PI/6.0),Math.cos(Math.PI/2.0)};

double F=235.0;

Force F1=new Force(F,cosfi1);

System.out.println(F1.toString());

double cosfi2[]={Math.cos(22.0*Math.PI/180.0),Math.cos(22.0*Math.PI/180.0),Math.cos(0.0)};

F1.turncosteta(cosfi2);

System.out.println(F1.toString());

}

}

 

public class forcetest2A

{

public static void main(String arg[])

{

double cosfi1[]={-Math.cos(Math.PI/3.0),Math.cos(Math.PI/6.0),Math.cos(Math.PI/2.0)};

double F=235.0;

Force F1=new Force(F,cosfi1);

System.out.println(F1.toString());

F1.turncosteta(Math.cos(22.0*Math.PI/180.0));

System.out.println(F1.toString());

}

}

 

---------- Capture Output ----------

> "c:\java\bin\javaw.exe" forcetest2A

P[0.0,0.0,0.0]

F[-117.50000000000003,203.5159698893431,1.43895998899814E-14]

M[0.0,0.0,0.0]

P[0.0,0.0,0.0]

F[-185.1825270975797,144.68044670152972,1.4389599889981402E-14]

M[0.0,0.0,0.0]

 

> Terminated with exit code 0.

 

public class forcetest3

{

public static void main(String arg[])

{

double cosfi1=Math.cos(140.0*Math.PI/180.0);

double cosfi2=Math.sin(140.0*Math.PI/180.0);

double FF=90.0;

double cosfi3[]={cosfi1,cosfi2,0.0};

Force F1=new Force(FF,cosfi3);

System.out.println("F1:\n"+F1.toString());

double cosfi4=Math.cos(Math.PI/6.0);

double RP=-F1.F[0]/cosfi4;

System.out.println("RP="+RP);

Force P=new Force(RP,cosfi4);

System.out.println("P:\n"+P.toString());

double RF=F1.F[1]+P.F[1]-20.0;

System.out.println("RF="+RF);

double cosfi5=Math.cos(3.0*Math.PI/2.0);

Force F=new Force(RF,cosfi5);

System.out.println("F:\n"+F.toString());

}

}

 

---------- Capture Output ----------

> "c:\java\bin\javaw.exe" forcetest3

F1:

P[0.0,0.0,0.0]

F[-68.943999880708,57.85088487178855,0.0]

M[0.0,0.0,0.0]

RP=79.60967378027259

P:

P[0.0,0.0,0.0]

F[68.943999880708,39.80483689013628,0.0]

M[0.0,0.0,0.0]

RF=77.65572176192484

F:

P[0.0,0.0,0.0]

F[-1.4265124663682806E-14,77.65572176192484,0.0]

M[0.0,0.0,0.0]

 

> Terminated with exit code 0.

 

public class forcetest4

{

public static void main(String arg[])

{

double x1[]={2,-4,1};

double F=100.0; //N

Force F1=new Force(x1,F);

double dcosteta[]=F1.costetaF();

System.out.println("F1:\n"+F1.toString());

System.out.println("F1:\n"+F1.toString("dcos",dcosteta));

}

 

---------- Capture Output ----------

> "c:\java\bin\javaw.exe" forcetest4

F1:

P[0.0,0.0,0.0]

F[43.64357804719848,-87.28715609439696,21.82178902359924]

M[0.0,0.0,0.0]

F1:

P[0.0,0.0,0.0]

dcos[0.4364357804719847,-0.8728715609439694,0.21821789023599236]

 

 

> Terminated with exit code 0.

 

A force couple applied with F1=100j  at p1[-5,0,0] and F2=-100j at p2[5,0,0] add the couple

public class forcetest5

{

public static void main(String arg[])

{

double x1[]={-5,0,0};

double F1[]={0,100.0,0}; //N

Force FA=new Force(x1,F1);

double x2[]={5,0,0};

double F2[]={0,-100.0,0}; //N

Force FB=new Force(x2,F2);

point p=new point(0,0,0);

FA.movetopoint(p,FB);

System.out.println("F1:\n"+FA.toString());

}

}

 

---------- Capture Output ----------

> "c:\java\bin\javaw.exe" forcetest5

F1:

P[0.0,0.0,0.0]

F[0.0,0.0,0.0]

M[0.0,0.0,1000.0]

 

> Terminated with exit code 0.

 

EXERCISE 2 class point2D, circle, cylinder

 

public class point2D

{

    public double x, y;

 

    public point2D(double a, double b)

    {x=a;y=b;}

   

    public point2D(point2D A)

    {x=A.x;

     y=A.y;

    }

 

    public String toString()

    {return "["+x+","+y+"]";}

   

    public String name(){return "point";}

 

}

 

 public class circle

{

    public double radius;

    public point2D p;

   

    public circle()

    {   p=new point2D(0.0,0.0);

        radius=0.0;}

 

    public circle(double r, double a, double b)

    {  p=new point2D(a,b);

       radius=r;

    }

   

    public circle(double r, point2D n)

    {  p=new point2D(n.x,n.y);

       radius=r;

    }

   

    public circle(circle c)

    {  p=new point2D(c.p.x,c.p.y);

       radius=c.radius;

    }

           

    public double area()

    {return Math.PI*radius*radius;}

 

    public String toString()

    { return "Center = "+"["+p.x+","+p.y+"]"+"; radius="+radius;}

   

    public String name(){return "circle";}

}

 

 public class cylinder

 {

 public double height;

 public circle c;

 

 public cylinder()

 {c=new circle(0.0,0.0,0.0);

  height=0.0;}

 

 public cylinder( double h,circle d)

 {c=new circle(d);

  height=h;

 }

 

 public cylinder( double h, double r, double a, double b)

 {c=new circle(r,a,b);

  height=h;

 }

 

 public double area()

 {return 2*c.area()+2*Math.PI*c.radius*height;}

 

 public double volume(){return c.area()*height;}

 

 public String toString()

 {return c.toString()+";height = "+height;}

 

 public String name(){return "cylinder";}

}

 

import javax.swing.*;

 

class compositiontest1

{

   public static void main(String args[])

   {

   point2D n=new point2D(1.0,2.0);

   circle d1=new circle(3.0,n);

   circle d2=new circle(4.0,2.0,3.0);

   cylinder s1=new cylinder(5.0,d1);

   cylinder s2=new cylinder(5.0,3.0,1.0,2.0);

   String s=n.toString()+"\n "+

   d1.name()+" "+d1.toString()+"\n "+

   d2.name()+" "+d2.toString()+"\n "+

   s1.name()+" "+s1.toString()+"\n "+

   s2.name()+" "+s2.toString()+"\n ";

   JOptionPane.showMessageDialog(null,s,

     "classes: point2D circle cylinder protected composition test",JOptionPane.PLAIN_MESSAGE);

     System.exit(0);}

}

 

EXERCISE 3 class vehicle, truck,bus, minibus

Program 7.1.10 vehicle.java : a simple class with public class variables

import javax.swing.*;

 

public class vehicle

{

//valid vehicle types bus,truck,minitruck,minivan,auto 

public String vehicletype;

public String brandName;

public double power; //KW olarak

public String color;

public double weight;            //yüksüz net ağırlık

//kurucu metodlar

 

public vehicle()

{vehicletype="not defined";

brandName="not defined";

power=0;

color="not defined";

weight=0;

}

 

public vehicle(String i_vehicletype,String i_brandName,double i_motor,String i_color,double i_weight)

{//vehicle type "bus","truck","mini truck","mini bus","auto","tır"

//if not define as "not defined"

if(i_vehicletype.equals("truck") ||

   i_vehicletype.equals("minitruck") ||

   i_vehicletype.equals("bus")||

   i_vehicletype.equals("minibus")||

   i_vehicletype.equals("auto") ||

   i_vehicletype.equals("motorcycle") ||

   i_vehicletype.equals("bicycle") ||

   i_vehicletype.equals("off road vehicle")

   )

{vehicletype=i_vehicletype;}

else

{vehicletype="not defined";}

brandName=i_brandName;

power=i_motor;

color=i_color;

weight=i_weight;

}

 

public vehicle(vehicle A)

{//gerekli tanımları yap

vehicletype=A.vehicletype;

brandName=A.brandName;

power=A.power;

color=A.color;

weight=A.weight;

}

 

public void define_vehicle(String i_vehicletype,String i_brandName,double i_motor,String i_color,double i_weight)

{//vehicle type "bus","truck","mini truck","mini bus","auto","tır"

//if not define as "not defined"

if(i_vehicletype.equals("truck") ||

   i_vehicletype.equals("minitruck") ||

   i_vehicletype.equals("bus")||

   i_vehicletype.equals("minibus")||

   i_vehicletype.equals("auto") ||

   i_vehicletype.equals("motorcycle") ||

   i_vehicletype.equals("bicycle") ||

   i_vehicletype.equals("off road vehicle"))

   {vehicletype=i_vehicletype;}

else

{vehicletype="not defined";}

brandName=i_brandName;

power=i_motor;

color=i_color;

weight=i_weight;

}

 

public String toString()

{String s1="";

s1+="Vehicle Type : "+vehicletype+"\n";

s1+="Brand Name : "+brandName+"\n";

s1+="Motor Power : "+power+" KW\n";

s1+="Color : "+color+"\n";

s1+="Weight : "+weight+" kg\n";

return s1;//gerekli tanımları yap

}

 

public void outputwindow()

{

String s1="Vehicle class output window";        

JOptionPane.showMessageDialog(null,toString(),s1, JOptionPane.PLAIN_MESSAGE);            

}

 

}

 

Program 7.1.11 truck.java : a simple class with public class variables, use class vehicle as class variable

public class truck

{

vehicle v;             

 

public truck(String i_marka,double i_motor,String i_renk,double i_agirlik)

{v=new vehicle("truck",i_marka,i_motor,i_renk,i_agirlik);}

 

public truck()

{ v=new vehicle();

  v.vehicletype="truck";

}

public truck(truck A)

{v=new vehicle(A.v);

 v.vehicletype="truck";

}

 

}

 

Program 7.1.12 bus.java : a simple class with public class variables, use class vehicle as class variable

public class bus

{

vehicle v;             

 

public bus(String i_marka,double i_motor,String i_renk,double i_agirlik)

{v=new vehicle("bus",i_marka,i_motor,i_renk,i_agirlik);}

 

public bus()

{ v=new vehicle();

  v.vehicletype="bus";

}

public bus(bus A)

{ v=new vehicle(A.v);

 v.vehicletype="bus";

 

}

 

}

 

Program 7.1.13 minibus.java : a simple class with public class variables, use class vehicle as class variable

public class minibus

{

vehicle v;             

 

public minibus(String i_marka,double i_motor,String i_renk,double i_agirlik)

{v=new vehicle("minibus",i_marka,i_motor,i_renk,i_agirlik);}

 

public minibus()

{ v=new vehicle();

  v.vehicletype="minibus";

}

public minibus(minibus A)

{v=new vehicle(A.v);

 v.vehicletype="minibus";

}

 

}

 

Program 7.1.14  A test program for classes vehicle,bus,truck and minibus

import javax.swing.*;

class vehicletesti1

{

 

public static void outputwindow(String s)

{

String s1="Vehicle class output window";        

JOptionPane.showMessageDialog(null,s,s1, JOptionPane.PLAIN_MESSAGE);          

}

              

public static void main(String args[])

{

vehicle truck=new vehicle("truck","MAN",200.0,"red",15000);

vehicle bus=new vehicle("bus","Mersedes-Chrysler",300.0,"white",10000);

vehicle a=new vehicle();

String s=truck.toString()+"\n"+bus.toString()+"\n"+a.toString();

outputwindow(s);

}

 

}

 

 

SECTION 2:  INHERITANCE

EXERCISE 1

 public class point

{

public double x[];

public point(double xi,double yi,double zi)

{ x=new double[3];

x[0]=xi;x[1]=yi;x[2]=zi;}

 

public point(double xi[])

{ x=new double[3];

for(int i=0;i<3;i++) x[i]=xi[i];

}

public point()

{ x=new double[3];

for(int i=0;i<3;i++) x[i]=0.0;

}

public point(point pi)

{x=new double[3];

for(int i=0;i<3;i++) x[i]=pi.x[i];}

 

public double R()

{return Math.sqrt(x[0]*x[0]+x[1]*x[1]+x[2]*x[2]);}

 

public double[] costeta()

{

double r=R();

double cos[]=new double[3];

for(int i=0;i<3;i++) cos[i]=x[i]/r;

return cos;

}

public double[] teta()

{

double teta[]=new double[3];

double costeta[]=costeta();

for(int i=0;i<3;i++) teta[i]=Math.acos(costeta[i]);

return teta;

}

public double[] dx(point pi)

{ double d[]=new double[3];

  for(int i=0;i<3;i++) d[i]=pi.x[i]-x[i];

  return d;

}

public double dR(point pi)

{ double total=0;

  for(int i=0;i<3;i++)

  {total+=(x[i]-pi.x[i])*(x[i]-pi.x[i]);}

               return Math.sqrt(total);

}

public double[] dcosteta(point pi)

{

double dr=dR(pi);

double dx[]=dx(pi);

double dcos[]=new double[3];

for(int i=0;i<3;i++) dcos[i]=dx[i]/dr;

return dcos;

}

public double[] dteta(point pi)

{

double dcos[]=dcosteta(pi);

double dt[]=new double[3];

for(int i=0;i<3;i++) dt[i]=Math.acos(dcos[i]);

return dt;

}

 

public String toString()

{String s="P["+x[0]+","+x[1]+","+x[2]+"]";

 return s;

}

 

public String name(){return "point";}

}

 

 public class Force extends point

{

public double F[];

public double M[];

 

public Force(double x[],double Fi[],double Mi[])

{ super(x);

  F=new double[3];

  M=new double[3];

  for(int i=0;i<3;i++) {F[i]=Fi[i];M[i]=Mi[i];}

}

 

public Force(double x[],double Fi[])

{ super(x);

  F=new double[3];

  M=new double[3];

  for(int i=0;i<3;i++) {F[i]=Fi[i];}

}

 

public Force(double Fi[])

{ super();

  F=new double[3];

  M=new double[3];

  for(int i=0;i<3;i++) {F[i]=Fi[i];}

}

 

public Force(double x[],double R,double costeta[])

{ super(x);

  F=new double[3];

  M=new double[3];

  for(int i=0;i<3;i++) {F[i]=R*costeta[i];}

}

//two points in space is given to define force

public Force(double x0[],double x1[],double R)

{ super(x0);

  point P1=new point(x1);

  double costeta[]=dcosteta(P1);

  F=new double[3];

  M=new double[3];

  for(int i=0;i<3;i++) {F[i]=R*costeta[i];}

}

//two points in space is given to define force

//first point 0,0,0 cartasian starting point

public Force(double x1[],double R)

{ super();

  point P1=new point(x1);

  double costeta[]=dcosteta(P1);

  F=new double[3];

  M=new double[3];

  for(int i=0;i<3;i++) {F[i]=R*costeta[i];}

}

 

public Force(double R,double costeta[])

{ super();

  F=new double[3];

  M=new double[3];

  for(int i=0;i<3;i++) {F[i]=R*costeta[i];}

}

//2D entry

public Force(double R,double costeta)

{ super();

  F=new double[3];

  M=new double[3];

  double tet=Math.acos(costeta);

  double costeta1[]={costeta,Math.sin(tet),0.0};

  for(int i=0;i<3;i++) {F[i]=R*costeta1[i];}

}

 

public Force(Force Fi)

{ super(Fi.x);

  F=new double[3];

  M=new double[3];

  for(int i=0;i<3;i++) {F[i]=Fi.F[i];M[i]=Fi.M[i];}

}

public double RF()

{return Math.sqrt(F[0]*F[0]+F[1]*F[1]+F[2]*F[2]);}

 

public double RM()

{return Math.sqrt(M[0]*M[0]+M[1]*M[1]+M[2]*M[2]);}

 

public double[] costetaF()

{

double r=RF();

double cos[]=new double[3];

for(int i=0;i<3;i++) cos[i]=F[i]/r;

return cos;

}

 

public double[] tetaF()

{

double r=RF();

double cos[]=new double[3];

for(int i=0;i<3;i++) cos[i]=Math.acos(F[i]/r);

return cos;

}

 

public double[] costetaM()

{

double r=RF();

double cos[]=new double[3];

for(int i=0;i<3;i++) cos[i]=M[i]/r;

return cos;

}

 

public double[] tetaM()

{

double r=RF();

double cos[]=new double[3];

for(int i=0;i<3;i++) cos[i]=Math.acos(M[i]/r);

return cos;

}

 

public void move(point p)

{double dp[]=dx(p);

 for(int i=0;i<3;i++) {x[i]=p.x[i];}

 M[0]+=dp[1]*F[2]-dp[2]*F[1];

 M[1]+=dp[2]*F[0]-dp[0]*F[2];

 M[2]+=dp[0]*F[1]-dp[1]*F[0];

}

 

public void movetopoint(point p,Force Fi)

{ add(Fi);

  move(p);

}

 

//Force addition

public void add(Force Fi)

{ point Pi=new point(Fi.x);

 double dp[]=dx(Pi);

 M[0]+=dp[1]*F[2]-dp[2]*F[1];

 M[1]+=dp[2]*F[0]-dp[0]*F[2];

 M[2]+=dp[0]*F[1]-dp[1]*F[0];

 for(int i=0;i<3;i++){F[i]+=Fi.F[i];}

}

//3D rotation

public void turnteta(double teta1[])

{double RF=RF();

 double teta2[]=tetaF();

 double teta[]=new double[3];

 for(int i=0;i<3;i++)

  {   teta[i]=teta1[i]+teta2[i];

                 F[i]=RF*Math.cos(teta[i]);

  }

}

 

//Plane(2D) rotation

public void turnteta(double teta1)

{double RF=RF();

 double teta2[]=tetaF();

 double teta3[]={teta1,teta1,0.0};

 double teta[]=new double[3];

 for(int i=0;i<3;i++)

  {   teta[i]=teta2[i]+teta3[i];

                 F[i]=RF*Math.cos(teta[i]);

  }

}

 

//3D rotation

public void turncosteta(double costeta2[])

{double RF=RF();

 double costeta1[]=costetaF();

 double teta1[]=new double[3];

 double teta2[]=new double[3];

 double teta[]=new double[3];

 for(int i=0;i<3;i++)

  {  teta1[i]=Math.acos(costeta1[i]);

     teta2[i]=Math.acos(costeta2[i]);

     teta[i]=teta1[i]+teta2[i];

                 F[i]=RF*Math.cos(teta[i]);}

}

 

//Plane rotation

public void turncosteta(double costeta)

{double RF=RF();

 double costeta1[]=costetaF();

 double costeta2[]={costeta,costeta,1.0};

 double teta[]=new double[3];

 double teta1[]=new double[3];

 double teta2[]=new double[3];

 for(int i=0;i<3;i++)

  {  teta1[i]=Math.acos(costeta1[i]);

     teta2[i]=Math.acos(costeta2[i]);

     teta[i]=teta1[i]+teta2[i];

                 F[i]=RF*Math.cos(teta[i]);}

}

 

public String toString()

{

String s=super.toString()+"\n";

s+="F["+F[0]+","+F[1]+","+F[2]+"]\n";   

s+="M["+M[0]+","+M[1]+","+M[2]+"]";

return s;

}

public String toString(String s1,double x[])

{

String s=super.toString()+"\n";

s+=s1+"["+x[0]+","+x[1]+","+x[2]+"]\n";             

return s;

}

}

 

public class forcetest1

{

public static void main(String arg[])

{

double cosfi1[]={Math.cos(Math.PI/2.0),Math.cos(0.0),Math.cos(Math.PI/2.0)};

double F=784.0;

Force F1=new Force(F,cosfi1);

System.out.println(F1.toString());

double cosfi2[]={Math.cos(20.0*Math.PI/180.0),Math.cos(20.0*Math.PI/180.0),Math.cos(0.0)};

F1.turncosteta(cosfi2);

System.out.println(F1.toString());

}

}

 

---------- Capture Output ----------

> "c:\java\bin\javaw.exe" forcetest1

P[0.0,0.0,0.0]

F[4.800615452657625E-14,784.0,4.800615452657625E-14]

M[0.0,0.0,0.0]

P[0.0,0.0,0.0]

F[-268.1437923673241,736.7190146961522,4.800615452657625E-14]

M[0.0,0.0,0.0]

 

> Terminated with exit code 0.

 

EXERCISE 2 class point2D, circle, cylinder

 

public class point2D

{

    public double x, y;

 

    public point2D(double a, double b)

    {x=a;y=b;}

   

    public point2D(point2D A)

    {x=A.x;

     y=A.y;

    }

 

    public String toString()

    {return "["+x+","+y+"]";}

   

    public String name(){return "point";}

 

}

 

 public class circle extends point2D

{

    public double radius;

   

    public circle()

    {   super(0.0,0.0);

        radius=0.0;

    }

 

    public circle(double r, double a, double b)

    {  super(a,b);

       radius=r;

    }

   

    public circle(double r, point2D n)

    {  super(n.x,n.y);

       radius=r;

    }

   

    public circle(circle c)

    {  super(c.x,c.y);

       radius=c.radius;

    }

           

    public double area()

    {return Math.PI*radius*radius;}

 

    public String toString()

    { return "Center = "+super.toString()+"; radius="+radius;}

    

    public String name(){return "circle";}

}

 

 public class cylinder extends circle

 {

 public double height;

 

 public cylinder()

 {super(0.0,0.0,0.0);

  height=0.0;}

 

 public cylinder( double h,circle d)

 {super(d);

  height=h;

 }

 

 public cylinder( double h, double r, double a, double b)

 {super(r,a,b);

  height=h;

 }

 

 public double area()

 {return 2*super.area()+2*Math.PI*radius*height;}

 

 public double volume(){return super.area()*height;}

 

 public String toString()

 {return super.toString()+";height = "+height;}

 

 public String name(){return "cylinder";}

}

 

import javax.swing.*;

 

class inheritancetest1

{

   public static void main(String args[])

   {

   point2D n=new point2D(1.0,2.0);

   circle d1=new circle(3.0,n);

   circle d2=new circle(4.0,2.0,3.0);

   cylinder s1=new cylinder(5.0,d1);

   cylinder s2=new cylinder(5.0,3.0,1.0,2.0);

   String s=n.toString()+"\n "+

   d1.name()+" "+d1.toString()+"\n "+

   d2.name()+" "+d2.toString()+"\n "+

   s1.name()+" "+s1.toString()+"\n "+

   s2.name()+" "+s2.toString()+"\n ";

   JOptionPane.showMessageDialog(null,s,

     "classes: point2D circle cylinder protected composition test",JOptionPane.PLAIN_MESSAGE);

     System.exit(0);}

}

 

import javax.swing.*;

 

class inheritancetest1A

{

   public static void main(String args[])

   { //indirect reference

   point2D n=new point2D(1.0,2.0);

   circle d1=new circle(3.0,n);

   circle d2=new circle(4.0,2.0,3.0);

   cylinder s1=new cylinder(5.0,d1);

   cylinder s2=new cylinder(5.0,3.0,1.0,2.0);

   point2D p[]=new point2D[5];

   p[0]=n;

   p[1]=d1;

   p[2]=d2;

   p[3]=s1;

   p[4]=s2;

   String s="";

   for(int i=0;i<p.length;i++) {s+=p[i].toString()+"\n";}

   JOptionPane.showMessageDialog(null,s,

     "classes: point2D circle cylinder protected composition test",JOptionPane.PLAIN_MESSAGE);

     System.exit(0);}

}

 

import javax.swing.*;

 

class inheritancetest1B

{  public static String toString(point2D x[])

   {String s="";

   for(int i=0;i<x.length;i++) {s+=x[i].toString()+"\n";}

   return s;

   }

  

   public  static void main(String args[])

   { //indirect reference

   point2D n=new point2D(1.0,2.0);

   circle d1=new circle(3.0,n);

   circle d2=new circle(4.0,2.0,3.0);

   cylinder s1=new cylinder(5.0,d1);

   cylinder s2=new cylinder(5.0,3.0,1.0,2.0);

   point2D p[]=new point2D[5];

   p[0]=n;

   p[1]=d1;

   p[2]=d2;

   p[3]=s1;

   p[4]=s2;

   String s="";

   for(int i=0;i<p.length;i++) {s+=p[i]+"\n";}

   JOptionPane.showMessageDialog(null,toString(p),

     "classes: point2D circle cylinder protected composition test",JOptionPane.PLAIN_MESSAGE);

     }

}

 

import javax.swing.*;

 

class inheritancetest1C

{  public static String toString(point2D x[])

   {String s="";

    //all classes inherited toString method

    // so instead of x[i].toString() x[i] is called

   for(int i=0;i<x.length;i++) {s+=x[i]+"\n";}

   return s;

   }

  

   public  static void main(String args[])

   { //indirect reference

   point2D n=new point2D(1.0,2.0);

   circle d1=new circle(3.0,n);

   circle d2=new circle(4.0,2.0,3.0);

   cylinder s1=new cylinder(5.0,d1);

   cylinder s2=new cylinder(5.0,3.0,1.0,2.0);

   point2D p[]=new point2D[5];

   p[0]=n;

   p[1]=d1;

   p[2]=d2;

   p[3]=s1;

   p[4]=s2;

   String s="";

   for(int i=0;i<p.length;i++) {s+=p[i]+"\n";}

   JOptionPane.showMessageDialog(null,toString(p),

     "classes: point2D circle cylinder protected composition test",JOptionPane.PLAIN_MESSAGE);

     System.exit(0);}

}

 

EXERCISE 3 class vehicle, truck,bus, minibüs, vehicle_register

import javax.swing.*;

 

public class vehicle

{

//valid vehicle types bus,truck,minitruck,minivan,auto 

public String vehicletype;

public String brandName;

public double power; //KW olarak

public String color;

public double weight;            //yüksüz net ağırlık

//kurucu metodlar

 

public vehicle()

{vehicletype="not defined";

brandName="not defined";

power=0;

color="not defined";

weight=0;

}

 

public vehicle(String i_vehicletype,String i_brandName,double i_motor,String i_color,double i_weight)

{//vehicle type "bus","truck","mini truck","mini bus","auto","tır"

//if not define as "not defined"

if(i_vehicletype.equals("truck") ||

   i_vehicletype.equals("minitruck") ||

   i_vehicletype.equals("bus")||

   i_vehicletype.equals("minibus")||

   i_vehicletype.equals("auto") ||

   i_vehicletype.equals("motorcycle") ||

   i_vehicletype.equals("bicycle") ||

   i_vehicletype.equals("off road vehicle")

   )

{vehicletype=i_vehicletype;}

else

{vehicletype="not defined";}

brandName=i_brandName;

power=i_motor;

color=i_color;

weight=i_weight;

}

 

public vehicle(vehicle A)

{//gerekli tanımları yap

vehicletype=A.vehicletype;

brandName=A.brandName;

power=A.power;

color=A.color;

weight=A.weight;

}

 

public void define_vehicle(String i_vehicletype,String i_brandName,double i_motor,String i_color,double i_weight)

{//vehicle type "bus","truck","mini truck","mini bus","auto","tır"

//if not define as "not defined"

if(i_vehicletype.equals("truck") ||

   i_vehicletype.equals("minitruck") ||

   i_vehicletype.equals("bus")||

   i_vehicletype.equals("minibus")||

   i_vehicletype.equals("auto") ||

   i_vehicletype.equals("motorcycle") ||

   i_vehicletype.equals("bicycle") ||

   i_vehicletype.equals("off road vehicle"))

   {vehicletype=i_vehicletype;}

else

{vehicletype="not defined";}

brandName=i_brandName;

power=i_motor;

color=i_color;

weight=i_weight;

}

 

public String toString()

{String s1="";

s1+="Vehicle Type : "+vehicletype+"\n";

s1+="Brand Name : "+brandName+"\n";

s1+="Motor Power : "+power+" KW\n";

s1+="Color : "+color+"\n";

s1+="Weight : "+weight+" kg\n";

return s1;//gerekli tanımları yap

}

 

public void outputwindow()

{

String s1="Vehicle class output window";        

JOptionPane.showMessageDialog(null,toString(),s1, JOptionPane.PLAIN_MESSAGE);            

}

 

}

 

 public class bus extends vehicle

{

 

public bus(String i_marka,double i_motor,String i_renk,double i_agirlik)

{super("bus",i_marka,i_motor,i_renk,i_agirlik);}

 

public bus()

{ super();

  vehicletype="bus";

}

public bus(bus A)

{ super(A);

  vehicletype="bus";

}

 

}

 

 public class minibus extends vehicle

{

 

public minibus(String i_marka,double i_motor,String i_renk,double i_agirlik)

{super("minibus",i_marka,i_motor,i_renk,i_agirlik);}

 

public minibus()

{ super();

  vehicletype="minibus";

}

public minibus(bus A)

{ super(A);

  vehicletype="minibus";

}

 

}

 

 public class truck  extends vehicle

{

 

public truck(String i_marka,double i_motor,String i_renk,double i_agirlik)

{super("truck",i_marka,i_motor,i_renk,i_agirlik);}

 

public truck()

{ super();

  vehicletype="truck";

}

public truck(truck A)

{super(A);

 vehicletype="truck";

}

 

}

 

A test program for classes vehicle,bus,truck and minibus

import javax.swing.*;

class vehicletesti1

{

 

public static void outputwindow(String s)

{

String s1="Vehicle class output window";        

JOptionPane.showMessageDialog(null,s,s1, JOptionPane.PLAIN_MESSAGE);          

}

              

public static void main(String args[])

{

vehicle truck=new vehicle("truck","MAN",200.0,"red",15000);

vehicle bus=new vehicle("bus","Mersedes-Chrysler",300.0,"white",10000);

vehicle a=new vehicle();

String s=truck.toString()+"\n"+bus.toString()+"\n"+a.toString();

outputwindow(s);

}

 

}

 

import javax.swing.*;

class vehicletest1A

{

 

public static void outputwindow(String s)

{

String s1="Vehicle class output window";              

JOptionPane.showMessageDialog(null,s,s1, JOptionPane.PLAIN_MESSAGE);         

}

              

public static void main(String args[])

{

vehicle truck=new vehicle("truck","MAN",200.0,"red",15000);

vehicle bus=new vehicle("bus","Mersedes-Chrysler",300.0,"white",10000);

vehicle a=new vehicle();

vehicle v[]=new vehicle[3];

v[0]=truck;

v[1]=bus;

v[2]=a;

String s="";

for(int i=0;i<v.length;i++)

{             s+=v[i].toString()+"\n";}

outputwindow(s);

}

 

}

 

public class vehicle_register

{

String name;

int number_of_vehicles;

vehicle list[];

 

public vehicle_register(String kname,vehicle klist[])

{name=kname;

 number_of_vehicles=klist.length;

 list=new vehicle[number_of_vehicles];

 for(int vehicle_number=0;vehicle_number<number_of_vehicles;vehicle_number++)

 {list[vehicle_number]=klist[vehicle_number];}

}

 

 public String toString()

 {            String s="Vehicle register name : "+name+"\n";

     s+="Total number of vehicles : "+number_of_vehicles+"\n\n";

     for(int vehicle_number=0;vehicle_number<number_of_vehicles;vehicle_number++)

     {s+=list[vehicle_number].toString()+"\n";}      

     return s;

 }

}

 

import javax.swing.*;

class vehicleregistertest1

{

public static void outputwindow(String s)

{

String s1="Vehicle class output window";              

JOptionPane.showMessageDialog(null,s,s1, JOptionPane.PLAIN_MESSAGE);         

}

              

public static void main(String args[])

{

vehicle v[]=new vehicle[3];

v[0]=new truck("MAN",200.0,"red",15000);

v[1]=new bus("Mersedes-Chrysler",300.0,"white",10000);

v[2]=new minibus("BMC",70,"blue",1070);

vehicle_register vr=new vehicle_register("izmir car registration",v);

String s=vr.toString()+"\n";

outputwindow(s);

}

 

}

 

EXERCISE 3 class book,novel,poetry,encyclopedia, library

public class book

{

String name;

String author;

String clasification;

String language;

 

public book(String kname,String kauthor,String kclasification,String klanguage)

{name=kname;

 author=kauthor;

 clasification=kclasification;

 language=klanguage;

}

 

public book(book k)

{name=k.name;

 author=k.author;

 clasification=k.clasification;

 language=k.language;

}

 

public String toString()

{

String s="name = "+name+"\n";

s+="author = "+author+"\n";

s+="book clasification = "+clasification+"\n";

s+="language = "+language+"\n";

return s;

}

 

}

 

public class fiction extends book

{

public fiction(String kisim,String kyazar,String klanguage)

{ super(kisim,kyazar,"fiction",klanguage);}

}

 

public class poetry extends book

{

public poetry(String kname,String kauthor,String klanguage)

{ super(kname,kauthor,"poetry",klanguage);}

 

}

 

public class encyclopedia extends book

{

public encyclopedia(String kname,String klanguage)

{ super(kname,"editing board","encyclopedia",klanguage);}

 

}

 

import javax.swing.*;

class booktest1

{

public static void outputwindow(String s)

{

String s1="book class output window";    

JOptionPane.showMessageDialog(null,s,s1, JOptionPane.PLAIN_MESSAGE);         

}

              

public static void main(String args[])

{

book magazine=new book("Bilim ve Teknik","TÜBİTAK","magazine","Turkish");

fiction r1=new fiction("It is all quiet in the westen front ","Erich Maria Reamarque","English");

book r2=new book("The God Delusion","Richard Dawkins","fiction","English");

poetry  s1=new poetry("Human lanscapes from my country","Nazım Hikmet","English and Turkish");

encyclopedia a1=new encyclopedia("Encyclopedia Britannica","English");

String s=magazine.toString()+"\n"+r1.toString()+"\n"+r2.toString()+"\n"+s1.toString()+"\n"+a1.toString();

outputwindow(s);

}}

 

import javax.swing.*;

class booktest1A

{

public static void outputwindow(String s)

{

String s1="book class output window";    

JOptionPane.showMessageDialog(null,s,s1, JOptionPane.PLAIN_MESSAGE);         

}

              

public static void main(String args[])

{

book magazine=new book("Bilim ve Teknik","TÜBİTAK","magazine","Turkish");

fiction r1=new fiction("It is all quiet in the westen front ","Erich Maria Reamarque","English");

book r2=new book("The God Delusion","Richard Dawkins","fiction","English");

poetry  s1=new poetry("Human lanscapes from my country","Nazım Hikmet","English and Turkish");

encyclopedia a1=new encyclopedia("Encyclopedia Britannica","English");

book b[]=new book[5];

b[0]=magazine;

b[1]=r1;

b[2]=r2;

b[3]=s1;

b[4]=a1;

String s="";

for(int i=0;i<b.length;i++)

{s+=b[i]+"\n";}

outputwindow(s);

}}

 

public class library

{

String name;

int number_of_books;

book list[];

 

public library(String kname,book klist[])

{name=kname;

 number_of_books=klist.length;

 list=new book[number_of_books];

 for(int book_number=0;book_number<number_of_books;book_number++)

 {list[book_number]=klist[book_number];}

}

 

 

 public String toString()

 {            String s="Library name : "+name+"\n";

     s+="Total number of books : "+number_of_books+"\n\n";

     for(int book_number=0;book_number<number_of_books;book_number++)

     {s+=list[book_number].toString()+"\n";}          

     return s;

 }

}

 

 

import javax.swing.*;

class booktest1B

{

public static void outputwindow(String s)

{

String s1="library class output window"; 

JOptionPane.showMessageDialog(null,s,s1, JOptionPane.PLAIN_MESSAGE);         

}

              

public static void main(String args[])

{            

book b[]=new book[5];

b[0]=new book("Bilim ve Teknik","TÜBİTAK","magazine","Turkish");

b[1]=new fiction("It is all quiet in the westen front ","Erich Maria Reamarque","English");

b[2]=new book("The God Delusion","Richard Dawkins","fiction","English");

b[3]=new poetry("Human lanscapes from my country","Nazım Hikmet","English and Turkish");

b[4]=new encyclopedia("Encyclopedia Britannica","English");

library l=new library("Library of congress",b);

String s=l.toString();

outputwindow(s);

}

}

 

EXERCISE 4 class Student, mechanical_Student, civil_Student, electrical_Student, course

 public class Student

{

private String firstName;

private String lastName;

private String StudentNumber;

private String school;

private double grade[];

private double weight[];

private double totalweight;

public Student( String first, String last, String sn,String school_name )

{

firstName = first;

lastName = last;

StudentNumber = sn;

if(school_name.equals("mechanical") || school_name.equals("chemical") ||

school_name.equals("electrical") || school_name.equals("industrial") ||

school_name.equals("civil") || school_name.equals("mining"))

{school=school_name;}

else

{school="not defined";}

}

public void setFirstName( String first )

{

firstName = first;

}

public String getFirstName()

{

return firstName;

}

 

public void setLastName( String last )

{

lastName = last;

}

 

public String getLastName()

{

return lastName;

}

 

public void setStudentNumber( String ssn )

{

StudentNumber = ssn;

}

 

public String getStudentNumber()

{

return StudentNumber;

}

public String getSchool()

{ return school; }

 

public void setGrade(double gradi[])

{int n=gradi.length;

grade=new double[n];

for(int i=0;i<n;i++)

{grade[i]=gradi[i];}

}

public void setWeight(double weighti[])

{int n=weighti.length;

weight=new double[n];

totalweight=0;

for(int i=0;i<n;i++)

{weight[i]=weighti[i];totalweight+=weight[i];}

}

public double[] getGrade()

{ return grade;

}

public double[] getWeight()

{ return weight;

}

public double getAverage()

{ double avg=0;

  int n=weight.length;

  for(int i=0;i<n;i++)

  {avg+=weight[i]*grade[i];}

  avg/=totalweight;

  return avg;

}

public String toString()

{ String s=getFirstName()+" "+getLastName()+" "+getStudentNumber()+" "+getSchool()+"\n";

 return s;

}

}

 

public class mechanical_Student extends Student

{

public mechanical_Student( String first, String last, String sn)

{ super(first,last,sn,"mechanical");

}

}

 

public class civil_Student extends Student

{

public civil_Student( String first, String last, String sn)

{ super(first,last,sn,"civil");

}

}

 

public class electrical_Student extends Student

{

public electrical_Student( String first, String last, String sn)

{ super(first,last,sn,"electrical");

}

}

 

 public class course

{

public String name;

public Student[] student;

private double weight[];

private double totalweight;

 

public course(String namei,Student[] studenti,double weighti[])

{

name=namei;

int n=studenti.length;

student=studenti;

setWeight(weighti);

for(int i=0;i<n;i++)

{student[i].setWeight(weighti);}

}

public void setWeight(double weighti[])

{int n=weighti.length;

weight=new double[n];

totalweight=0;

for(int i=0;i<n;i++)

{weight[i]=weighti[i];totalweight+=weight[i];}

}

public void setGrade(double grade[][])

{int n=weight.length;

 int m=student.length;

 for(int i=0;i<m;i++)

 {student[i].setGrade(grade[i]);} 

}

public String studentList()

{int n=student.length;

 String s="";

 for(int i=0;i<n;i++)

{s+=student.toString();}

return s;

}

 

}

 

import javax.swing.*;

public class coursetest

{

public static void main(String arg[])

{

Student s1[]=new Student[4];

s1[0]=new Student("Ali","Altın","123456","civil");

s1[1]=new mechanical_Student("Veli","Küçük","223457");

s1[2]=new mechanical_Student("Ayşe","Yılmaz","223458");

s1[3]=new electrical_Student("Fatma","Yıldız","323459");

double weight[]={0.3,0.3,0.4};

Class c1=new Class("Computer Programming I",s1,weight);

String s=c1.studentList();

JOptionPane.showMessageDialog(null,s1);

}

 

}

 

ABSTRACT CLASS AND INTERPHASE

EXERCISE 1 ABSTRACT CLASS

 public abstract class shape

{ public double area(){return 0.0;}

  public double volume() {return 0.0;}

  public abstract String name();

}

 

 public class point2D extends shape

{

    public double x, y;

 

    public point2D(double a, double b)

    {x=a;y=b;}

   

    public point2D(point2D A)

    {x=A.x;

     y=A.y;

    }

 

    public String toString()

    {return "["+x+","+y+"]";}

   

    public String name(){return "point";}

 

}

 

public class circle extends point2D

{  

    public double radius;

   

    public circle()

    {   super(0.0,0.0);

        radius=0.0;

    }

 

    public circle(double r, double a, double b)

    {  super(a,b);

       radius=r;

    }

   

    public circle(double r, point2D n)

    {  super(n.x,n.y);

       radius=r;

    }

   

    public circle(circle c)

    {  super(c.x,c.y);

       radius=c.radius;

    }

           

    public double area()

    {return Math.PI*radius*radius;}

 

    public String toString()

    { return "Center = "+super.toString()+" radius="+radius+" area="+area();}

   

    public String name(){return "circle";}

}

 

public class cylinder extends circle

 {

 public double height;

 

 public cylinder()

 {super(0.0,0.0,0.0);

  height=0.0;}

 

 public cylinder( double h,circle d)

 {super(d);

  height=h;

 }

 

 public cylinder( double h, double r, double a, double b)

 {super(r,a,b);

  height=h;

 }

 

 public double area()

 {return 2*super.area()+2*Math.PI*radius*height;}

 

 public double volume(){return super.area()*height;}

 

 public String toString()

 {return super.toString()+" height = "+height+" area="+area()+" volume="+volume();}

 

 public String name(){return "cylinder";}

}

 

public class cone extends circle

 {

 public double height;

 

 public cone()

 {super(0.0,0.0,0.0);

  height=0.0;}

 

 public cone( double h,circle d)

 {super(d);

  height=h;

 }

 

 public cone( double h, double r, double a, double b)

 {super(r,a,b);

  height=h;

 }

 

 public double area()

 {   double s=Math.sqrt(radius*radius+height*height);

                return super.area()+Math.PI*radius*s;}

 

 public double volume(){return 1.0/3.0*Math.PI*radius*radius*height;}

 

 public String toString()

 {{return super.toString()+" height = "+height+" area="+area()+" volume="+volume();}}

 

 public String name(){return "cone";}

}

 

import javax.swing.*;

class H9Ex1 {

public static String output(shape s[])

{String s1="";

 for(int i=0;i<s.length;i++)

 {s1+=s[i].name()+" : "+s[i].toString()+"\n";}

return s1;

}

 

public static void main(String args[])

{point2D   n   = new point2D(7,11);

 circle    d   = new circle(3.5,22,8);

 cylinder  s   = new cylinder(10,3.3,10,10);

 cone      c   = new cone(10,3.3,10,10);

 shape     a[] = {n,d,s,c};

 String s1="indirect referance use with abstract class\n";

 JOptionPane.showMessageDialog(null,output(a),s1,JOptionPane.PLAIN_MESSAGE);

}

}

 

EXERCISE 2 ABSTRACT CLASS

abstract class f_x

{ abstract double func(double x);}

 

 public class numerical

{

                             

public static double derivative(f_x f,double x)

{  double h=0.00001;

  return (-f.func(x+2.0*h)+8.0*f.func(x+h)-8.0*f.func(x-h)+f.func(x-2.0*h))/(12.0*h);

}

 

public static double integral(f_x f_xnt,double a,double b)

{

//integral f(x)dx boundaries a and b

 double r[]={-0.973906528517171,-0.865063366688984,-0.679409568299024,-0.433395394129247,-0.148874338981631,

 0.148874338981631,0.433395394129247,0.679409568299024,0.865063366688984,0.973906528517171};

 double c[]={0.066671344308684,0.149451349150580,0.219086362515982,0.269266719309996,0.295524224714752,

 0.295524224714752,0.269266719309996,0.219086362515982,0.149451349150580,0.066671344308684};

double z=0,x,y;

double k1=(b-a)/2.0;

double k2=(b+a)/2.0;

for(int i=0;i<r.length;i++)

{

x=k2+k1*r[i];

y=f_xnt.func(x);

z+=k1*c[i]*y;

}

return z;

}

}

 

import javax.swing.*;

 

class fb extends f_x

{ public double func(double x)

   { return x*x-5.0;}}

 

public class H9Ex2 {

public static void main(String args[])

{ fb ff=new fb();

  double x=1.0;

 String s="x ="+x+"\n derivative df/dx = "+numerical.derivative(ff,x);

 String s1=" indirect referencing and abstract class example :";

 JOptionPane.showMessageDialog(null,s,s1,JOptionPane.PLAIN_MESSAGE);

}

}

 

import javax.swing.*;

 

class fb extends f_x

{ public double func(double x)

   { return x*x-5.0;}}

  

class fc extends f_x

{ public double func(double x)

   { return x*x*3-5.0*x+2;}}

     

 

public class H9Ex2a {

 

public static void main(String args[])

{ 

    double x=1.0;

               String s="x ="+x+"\n derivative df/dx [x*x-5.0] = "+numerical.derivative(new fb(),x)+"\n";

               x=2;

               s+="x ="+x+"\n derivative df/dx [x*x-5.0] = "+numerical.derivative(new fb(),x)+"\n";

               x=1;

               s+="x ="+x+"\n derivative df/dx [x*x*3-5.0x+2] = "+numerical.derivative(new fc(),x)+"\n";

               x=2;

    s+="x ="+x+"\n derivative df/dx [x*x*3-5.0x+2] = "+numerical.derivative(new fc(),x)+"\n";

  

   String s1=" indirect referencing and abstract class example :";

   JOptionPane.showMessageDialog(null,s,s1,JOptionPane.PLAIN_MESSAGE);

}

}

 

import javax.swing.*;

 

class fb extends f_x

{ public double func(double x)

   { return x*x-5.0;}}

  

class fc extends f_x

{ public double func(double x)

   { return x*x*3-5.0*x+2;}}

     

 

public class H9Ex2b {

 

public static void main(String args[])

{ 

   double x,y1,y2;

   f_x f1=new fb();

   f_x f2=new fc();

   String s="";

   for(int i=0;i<20;i++)

   {x=0.1*i;

    y1=f1.func(x);

    y2=f2.func(x);

    s+="x="+x+"y1="+y1+"y2="+y2+"\n";

   } 

   String s1=" indirect referencing and abstract class example :";

   JOptionPane.showMessageDialog(null,s,s1,JOptionPane.PLAIN_MESSAGE);

}

}

 

EXERCISE 3 INTERFACE

public interface shapeI

{public double area();

 public double volume();

 public String name();

}

 

public class point2DI implements shapeI

{

    public double x, y;

 

    public point2DI(double a, double b)

    {x=a;y=b;}

   

    public point2DI(point2D A)

    {x=A.x;

     y=A.y;

    }

    public double area(){return 0.0;}

    public double volume(){return 0.0;}

    public String toString()

    {return "["+x+","+y+"]";}

   

    public String name(){return "point";}

 

}

 

public class circleI implements shapeI

{   public double x, y;

    public double radius;

   

    public circleI()

    {   x=0.0;

        y=0.0;

        radius=0.0;

    }

 

    public circleI(double r, double a, double b)

    {  x=a;

       y=b;

       radius=r;

    }

   

    public circleI(double r, point2D n)

    {  x=n.x;

       y=n.y;

       radius=r;

    }

   

    public circleI(circle c)

    {  x=c.x;

       y=c.y;

       radius=c.radius;

    }

           

    public double area()

    {return Math.PI*radius*radius;}

    public double volume(){return 0.0;}

    public String toString()

    { return "Center = "+super.toString()+" radius="+radius+" area="+area();}

   

    public String name(){return "circle";}

}

 

public class cylinderI implements shapeI

 { public double x, y;

   public double radius;

   public double height;

 

 public cylinderI()

 {x=0.0;

  y=0.0;

  radius=0.0;

  height=0.0;}

 

 public cylinderI( double h,circleI d)

 {x=d.x;

  y=d.y;

  radius=d.radius;

  height=h;

 }

 

 public cylinderI( double h, double r, double a, double b)

 {x=a;

  y=b;

  radius=r;

  height=h;

 }

 

 public double area()

 {return 2*Math.PI*radius*radius+2*Math.PI*radius*height;}

 

 public double volume(){return Math.PI*radius*radius*height;}

 

 public String toString()

 {return super.toString()+" height = "+height+" area="+area()+" volume="+volume();}

 

 public String name(){return "cylinder";}

}

 

public class coneI implements shapeI

 {

   public double x, y;

   public double radius;

   public double height;

 

 public coneI()

 {x=0.0;

  y=0.0;

  radius=0.0;

  height=0.0;}

 

 public coneI( double h,circleI d)

 {x=d.x;

  y=d.y;

  radius=d.radius;

  height=h;

 }

 

 public coneI( double h, double r, double a, double b)

 {x=a;

  y=b;

  radius=r;

  height=h;

 }

 

 public double area()

 {   double s=Math.sqrt(radius*radius+height*height);

                return Math.PI*radius*radius+Math.PI*radius*s;}

 

 public double volume(){return 1.0/3.0*Math.PI*radius*radius*height;}

 

 public String toString()

 {{return super.toString()+" height = "+height+" area="+area()+" volume="+volume();}}

 

 public String name(){return "cone";}

}

 

import javax.swing.*;

class H9Ex3 {

public static String output(shapeI s[])

{String s1="";

 for(int i=0;i<s.length;i++)

 {s1+=s[i].name()+" : "+s[i].toString()+"\n";}

return s1;

}

 

public static void main(String args[])

{point2DI   n   = new point2DI(7,11);

 circleI    d   = new circleI(3.5,22,8);

 cylinderI  s   = new cylinderI(10,3.3,10,10);

 coneI      c   = new coneI(10,3.3,10,10);

 shapeI     a[] = {n,d,s,c};

 String s1="indirect referance use with interface\n";

 JOptionPane.showMessageDialog(null,output(a),s1,JOptionPane.PLAIN_MESSAGE);

}

}

 

EXERCISE 4 INTERFACE

public interface f_xI

{ double func(double x);}

 

public class numericalI

{

                             

public static double derivative(f_xI f,double x)

{  double h=0.00001;

  return (-f.func(x+2.0*h)+8.0*f.func(x+h)-8.0*f.func(x-h)+f.func(x-2.0*h))/(12.0*h);

}

 

public static double integral(f_xI f_xnt,double a,double b)

{

//integral f(x)dx boundaries a and b

 double r[]={-0.973906528517171,-0.865063366688984,-0.679409568299024,-0.433395394129247,-0.148874338981631,

 0.148874338981631,0.433395394129247,0.679409568299024,0.865063366688984,0.973906528517171};

 double c[]={0.066671344308684,0.149451349150580,0.219086362515982,0.269266719309996,0.295524224714752,

 0.295524224714752,0.269266719309996,0.219086362515982,0.149451349150580,0.066671344308684};

double z=0,x,y;

double k1=(b-a)/2.0;

double k2=(b+a)/2.0;

for(int i=0;i<r.length;i++)

{

x=k2+k1*r[i];

y=f_xnt.func(x);

z+=k1*c[i]*y;

}

return z;

}

}

 

import javax.swing.*;

 

class fb implements f_xI

{ public double func(double x)

   { return x*x-5.0;}}

 

public class H9Ex4 {

public static void main(String args[])

{ fb ff=new fb();

  double x=1.0;

 String s="x ="+x+"\n derivative df/dx = "+numericalI.derivative(ff,x);

 String s1=" indirect referencing and interface example :";

 JOptionPane.showMessageDialog(null,s,s1,JOptionPane.PLAIN_MESSAGE);

}

}

 

Lambda expression version

import javax.swing.*;

 

public class H9Ex4A {

public static void main(String args[])

{  //lambda expression

  f_xI ff=x->x*x-5.0;

  double x1=1.0;

 String s="x ="+x1+"\n derivative df/dx = "+numericalI.derivative(ff,x1);

 String s1=" indirect referencing and interface example :";

 JOptionPane.showMessageDialog(null,s,s1,JOptionPane.PLAIN_MESSAGE);

}

}

 

EXERCISE 5 ABSTRACT CLASS

public class MathT

{

public static double PI=3.1415926535898;

public static double E=2.718281828459;

     public static double exp(double x)

      { // number e = 2.718...

        // exp(x) = 1 + x /1! + x2 / 2! + x3 / 3! + x4 / 4! + .double power=1;

        double exponent=1;

        double factorial=1;

        int number=1;

        double power=1;

        while(number<=300)

        {

        factorial*=number;

        power*=x;

        exponent+=power/factorial;

        number++;

        }

      return exponent;

      }

      public static double log(double x)

      {double y=(x-1.0)/(x+1.0);

        double power=2.0*y;

        double z=y*y;

        int n=0;

        double ln=0;

        while(n<=300)

        {

        ln+=1./(2.0*n+1.0)*power;

        power*=z;

        n++;

        }

     return ln;

     }

     public static double log10(double x)

     { return log(x)/log(10.0);}

     public static double sqrt(double x)

     { return exp(0.5*log(x));}

     public static double sqr(double x)

     { return x*x;}

     public static double cubrt(double x)

     { return exp((1.0/3.0)*log(x));}

     public static double pow(double a,double x)

     { return exp(x*log(a));}

     public static double root(double a,double x)

     { return exp((1.0/x)*log(a));}

     public static double sin(double x)

     {double sum=x;

      double pow=x;

      double fact=1;

      int plus_minus=-1;

      int kp=0;

      for(int n=1;n<100;n++)

      {   pow*=(x*x);

                 fact*=(2*n+1)*(2*n);

                     sum+=plus_minus*pow/fact;

                     plus_minus*=-1;

      }

      return sum;  

      }

      public static double sinh(double x)

     {double sum=x;

      double pow=x;

      double fact=1;

      int kp=0;

      for(int n=1;n<150;n++)

      {   pow*=(x*x);

                 fact*=(2*n+1)*(2*n);

                     sum+=pow/fact;

      }

      return sum;  

      }

     public static double cos(double x)

     {double sum=1;

      double pow=1;

      double fact=1;

      int plus_minus=-1;

      int kp=0;

      for(int n=1;n<150;n++)

      {   pow*=(x*x);

                 fact*=(2*n)*(2*n-1);

                     sum+=plus_minus*pow/fact;

                     plus_minus*=-1;

      }

      return sum;  

      }

     public static double cosh(double x)

     {double sum=1;

      double pow=1;

      double fact=1;

      int kp=0;

      for(int n=1;n<150;n++)

      {   pow*=(x*x);

                 fact*=(2*n)*(2*n-1);

                     sum+=pow/fact;

      }

      return sum;  

      }

      public static double tan(double x)

      {return sin(x)/cos(x);}

      public static double tanh(double x)

      {return sinh(x)/cosh(x);}

      //error function     

      public static double erf(double x)

      { double sum=x;

        double pow=x;

        double fact=1;

        int plus_minus=-1;

        for(int n=1;n<100;n++)

        {   pow*=(x*x);

                       sum+=plus_minus*pow/fact/(2.0*n+1.0);

                       fact*=n;

                       plus_minus*=-1;

        }

        sum*=1.0/Math.sqrt(Math.PI);

        return sum;

        }

        public static double asin(double x)

        { double sum=x;

          double pow=x;

          double fact=1;

          double fact2=1;

          double twok=1.0;

          for(int k=1;k<60;k++)

          {   pow*=(x*x);

              fact*=k;

              fact2*=(2*k)*(2*k-1);

              twok*=4.0;

                         sum+=fact2*pow/(fact*fact*twok*(2.0*k+1.0));

          }

          return sum;             

        }

        public static double acos(double x)

        {return PI/2.0-asin(x);}

        public static double asinh(double x)

        { double sum=x;

          double pow=x;

          double fact=1;

          double fact2=1;

          double twok=1.0;

          int plus_minus=-1;

          for(int k=1;k<60;k++)

          {   pow*=(x*x);

              fact*=k;

              fact2*=(2*k)*(2*k-1);

              twok*=4.0;

                         sum+=plus_minus*fact2*pow/(fact*fact*twok*(2.0*k+1.0));

                         plus_minus*=-1;

          }

          return sum;             

        }

        public static double atan(double x)

        { double sum=x;

          double pow=x;

          int plus_minus=-1;

          for(int k=1;k<100;k++)

          {   pow*=(x*x);

                         sum+=plus_minus*pow/(2.0*k+1.0);

                         plus_minus*=-1;

          }

          return sum;             

        }

        public static double atanh(double x)

        { double sum=x;

          double pow=x;

          for(int k=1;k<100;k++)

          {   pow*=(x*x);

                         sum+=pow/(2.0*k+1.0);

          }

          return sum;             

         }

}

 

abstract class f_xT extends MathT

{ abstract double func(double x);}

 

public class numericalT

{

                             

public static double derivative(f_xT f,double x)

{  double h=0.000001;

  return (-f.func(x+2.0*h)+8.0*f.func(x+h)-8.0*f.func(x-h)+f.func(x-2.0*h))/(12.0*h);

}

 

public static double integral(f_xT f_xnt,double a,double b)

{

//integral f(x)dx boundaries a and b

 double r[]={-0.973906528517171,-0.865063366688984,-0.679409568299024,-0.433395394129247,-0.148874338981631,

 0.148874338981631,0.433395394129247,0.679409568299024,0.865063366688984,0.973906528517171};

 double c[]={0.066671344308684,0.149451349150580,0.219086362515982,0.269266719309996,0.295524224714752,

 0.295524224714752,0.269266719309996,0.219086362515982,0.149451349150580,0.066671344308684};

double z=0,x,y;

double k1=(b-a)/2.0;

double k2=(b+a)/2.0;

for(int i=0;i<r.length;i++)

{

x=k2+k1*r[i];

y=f_xnt.func(x);

z+=k1*c[i]*y;

}

return z;

}

}

 

import javax.swing.*;

 

class fb extends f_xT

{ public double func(double x)

   { return sin(x);}}

 

public class H9Ex5 {

public static void main(String args[])

{ fb ff=new fb();

  double x=MathT.PI/3.0;

 String s="sin("+x+") ="+ff.func(x)+"\n derivative df/dx = "+numericalT.derivative(ff,x);

 String s1=" indirect referencing and abstract class example :";

 JOptionPane.showMessageDialog(null,s,s1,JOptionPane.PLAIN_MESSAGE);

}

}

 

HOMEWORKS

HOMEWORK 1)

 

HOMEWORK 2)

 investigate  classes point, circle, and cylinder. Create a cone.java classes, extended from class circle that will be calculated area and volume of a cone (cone.java) . Note that this class will be similar to cyclinder class except the way of calculating area and volume 

Area of the conic surface = prs

Area of the base= pr2

Area of the cone = prs+ pr2

Volume of the cone = 1/3 pr2h

s = (r2 + h2)0.5

 

HOMEWORK   3 )  Investigate classes Student and course. Add course class a method to calculate avarage grades of all students in the class and list the results.          

HOMEWORK 4)  From abstract class shape derive class sphere. This class should calculate its area and volume , Test it by using indirect reference shape 

HOMEWORK 5)  From interface shapeI derive class sphere. This class should calculate its area and volume , Test it by using indirect reference shape 

 

HOMEWORK 6) Write a test program to read value of x from input then calculate derivative of function   y=e-x/(x2-1) in a test program by using lambda expressions

HOMEWORK 7) Write a test program to read value of a and b (lower and upper limit of the integral) from input then calculate integral of function

 y=e-x/(x2-1) in a test program