W7_COMPUTER PROGRAMMINING 2020 SPRING

W7 Further class concepts, polymorphism, abstract class, composition, inheritance, indirect referencing, lambda variables

 

EX 1 Composition

 import javax.swing.*;

 //Composition

class f1

{ public double func(double x)

  {return x*x-2.0;}

  public double dfunc(double x)

  { double dx=0.001;

    double df=(func(x+dx)-func(x-dx))/(2.0*dx);

    return df;

  }

 

}

public class W7Ex1

{ public static double Dinput(String s)

  {     return Double.parseDouble(JOptionPane.showInputDialog(s));}

  public static double secant(f1 f,double x)

  { double eps=1.e-6;

    int i=0;

    int nmax=100;

    while(Math.abs(f.func(x))>eps && i<nmax)

    {x=x-f.func(x)/f.dfunc(x);i++;}

    return x;

  }

  public static void main(String arg[])

  { //root of a function

                double x0=Dinput("x0=");

    f1 f=new f1();

    double x=secant(f,x0);

    String s="x="+x;

    JOptionPane.showMessageDialog(null,s);

  }

}

 

 import javax.swing.*;

 //Composition

class f1

{

}

public class W7Ex1A

{ public static double func(double x)

  {return x*x-2.0;}

  public static double dfunc(double x)

  { double dx=0.001;

    double df=(func(x+dx)-func(x-dx))/(2.0*dx);

    return df;

  }

  public static double Dinput(String s)

  {     return Double.parseDouble(JOptionPane.showInputDialog(s));}

  public static double secant(double x)

  { double eps=1.e-6;

    int i=0;

    int nmax=100;

    while(Math.abs(func(x))>eps && i<nmax)

    {x=x-func(x)/dfunc(x);i++;}

    return x;

  }

  public static void main(String arg[])

  { //root of a function

                double x0=Dinput("x0=");

    f1 f=new f1();

    double x=secant(x0);

    String s="x="+x;

    JOptionPane.showMessageDialog(null,s);

  }

}

 

EX2

Abstract class  f_x.java

//abstract class

abstract public class f_x

{abstract public double func(double x);

 public double dfunc(double x)

  { double dx=0.001;

    double df=(func(x+dx)-func(x-dx))/(2.0*dx);

    return df;

  }

}

 

 

 import javax.swing.*;

 class f1 extends f_x

{ public double func(double x)

  {return x*x-2.0;}

}

 

public class W7Ex2

{ public static double secant(f_x f,double x)

  { double eps=1.e-6;

    int i=0;

    int nmax=100;

    while(Math.abs(f.func(x))>eps && i<nmax)

    {x=x-f.func(x)/f.dfunc(x);i++;}

    return x;

  }

  public static void main(String arg[])

  { //root of a function

                double x0=W7Ex1.Dinput("x0=");

    f1 f=new f1();

    double x=secant(f,x0);

    String s="x="+x;

   JOptionPane.showMessageDialog(null,s);

  }

}

 

 import javax.swing.*;

class f2 extends f_x

{ public double func(double x)

  {return x*x-x-2.0*x+5;}

}

 

public class W7Ex2A

{

  public static void main(String arg[])

  { //root of a function

                double x0=W7Ex1.Dinput("x0=");

    f1 f=new f1();

    double x=W7Ex2.secant(f,x0);

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

    f2 ff=new f2();

    x=W7Ex2.secant(ff,x0);

    s+="x="+x;

    JOptionPane.showMessageDialog(null,s);

  }

}

 

EX3

Interface if_x.java

interface if_x

{  public double func(double x);

//first order derivative

default double dfunc(double x)

{double dx=0.001;

    double df=(func(x+dx)-func(x-dx))/(2.0*dx);

    return df;

}

}

 

 import javax.swing.*;

class f3 implements if_x

{ public double func(double x)

  {return x*x-2.0;}

}

 

public class W7Ex3

{ public static double secant(if_x f,double x)

  { double eps=1.e-6;

    int i=0;

    int nmax=100;

    while(Math.abs(f.func(x))>eps && i<nmax)

    {x=x-f.func(x)/f.dfunc(x);i++;}

    return x;

  }

  public static void main(String arg[])

  { //root of a function

                double x0=W7Ex1.Dinput("x0=");

    f3 f=new f3();

    double x=secant(f,x0);

    String s="x="+x;

    JOptionPane.showMessageDialog(null,s);

  }

}

 

 class f4 implements if_x

{ public double func(double x)

  {return x*x-x-2.0*x+5;}

}

 

public class W7Ex3A

{

  public static void main(String arg[])

  { //root of a function

                double x0=W7Ex1.Dinput("x0=");

    f3 f=new f3();

    double x=W7Ex3.secant(f,x0);

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

    f4 ff=new f4();

    x=W7Ex3.secant(ff,x0);

    s+="x="+x;

    JOptionPane.showMessageDialog(null,s);

  }

}

 

EX4 Lambda variables

 public class W7E4

{

  public static void main(String arg[])

  { //root of a function

                double a=IO.Dinput("a=");

    double b=IO.Dinput("a=");

    if_x f=x->x*x-2.3*x-2.0;

    double x0=bisection(f,a,b);

    String s="x0="+x0;

    IO.print(s);

  }

}

 

import javax.swing.*;

public class W7Ex4A

{ public static double integral(if_x f,double a,double 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};

int n=r.length;

double z=0;

double x,y;

double k1=(b-a)/2.0;

double k2=(b+a)/2.0;

double y1=0;

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

{x=k2+k1*r[i];y=f.func(x);y1=c[i]*y;z+=y1;}

return k1*z;

}

 

  public static void main(String arg[])

  { //integral of a function

                double a=W7Ex1.Dinput("a=");

                double b=W7Ex1.Dinput("b=");

    if_x f=x->x*x-2.0;

    double I=integral(f,a,b);

    String s="I="+I;

    JOptionPane.showMessageDialog(null,s);

  }

}

 

EX5 Lambda variables

interface if_xy

{public double func(double x[]);}

 

 import javax.swing.*;

 public class W7Ex4

{

  public static void main(String arg[])

  { //root of a function

                //root of a function

                double x0=W7Ex1.Dinput("x0=");

    if_x f=x->x*x-2.0;

    double x=W7Ex3.secant(f,x0);

    String s="x="+x;

    JOptionPane.showMessageDialog(null,s);

  }}

 

import javax.swing.*;

class f6 implements if_xy

{public double func(double z[])

 {double w=z[0]*z[0]+z[1]*z[1];

  return w;

 }

}

public class W7Ex5A

{

  public static void main(String arg[])

  { //root of a function

    double x=W7Ex1.Dinput("x=");

    double y=W7Ex1.Dinput("y=");

   f6 f=new f6();

    double z[]={x,y};

    String s="x="+z[0]+"y="+z[1]+"\nx*x+y*y = "+f.func(z);

    JOptionPane.showMessageDialog(null,s);

  }

}

 

 

HOMEWORK EXERCISES

computer_programming@turhancoban.com

W1_turhan_coban_0101333.pdf

W3_ali_veli_02335646.pdf

 

W7HW1 : abstract class y_x is given calculate function value and derivative of y(x)= x*x-2.3*x-2.0;

//abstract class y_x.java

abstract public class y_x

{abstract public double func(double x);

 //derivative of func

 public double dfunc(double x)

 {double h=0.0001;

  double dy=(func(x+h)-func(x-h))/(2.0*h);

  return dy;

 }

}

 

 class y1 extends y_x

{ public double func(double x)

  {return x*x-2.3*x-2.0;}

}

public class W7HW1

{

  public static void main(String arg[])

  {

  }

}

 

W7HW2 Interface iy_x.java is given calculate function value and derivative of y(x)= x*x-2.3*x-2.0;

@FunctionalInterface

interface iy_x

{  public double func(double x);

//first order derivative

default double dfunc(double x)

{double h=1.0e-3;

 double dy=(func(x+h)-func(x-h))/(2.0*h);

 return dy;

 }

}

 

class y1 implements iy_x

{ public double func(double x)

  {return x*x-2.3*x-2.0;}

}

public class W7HW2

{

  public static void main(String arg[])

  {

  }

}

 

W7HW3 Interface iy_x.java is given calculate function value and derivative of y(x)= x*x-2.3*x-2.0 by using lambda variables

 

W7HW4 Interface iz_xy is given as

@FunctionalInterface

interface iz_xy

{public double func(double x[]);

default double[] dfunc(double x[])

{double h=1.0e-3;

int n=x.length;

double dy[]=new double[n];

double z1[]=new double[n];

double z2[]=new double[n];

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

{ for(int j=0;j<n;j++) {z1[i]=x[i];z2[i]=x[i];}

 z1[i]=x[i]+h;

 z2[i]=x[i]-h;

 dy[i]=(func(z1)-func(z2))/(2.0*h);

 z1[i]=x[i];z2[i]=x[i];

}

return dy;

}

}

 

public class W7HW4

{

  public static void main(String arg[])

  { }

 

Calculate

Z=2x+3xy+3.3*x2-1.23y2

z=2.0*x[0]+3.0*x[0]*x[1]+3.3*x[0]*x[0]-1.23*x[1]*x[1]

function value and its derivatives by using lambda variables

 

W7HW5 Calculate (use lambda variables)