W4 COMPUTER PROGRAMMINING 2020 SPRING

W4 Introduction to class concept & dynamic methods

 

We will use the class IO, be sure that it is include in the directory you use

import java.util.Scanner;

import javax.swing.*;

 

class IO

{  public static void print(String s)

   {JOptionPane.showMessageDialog(null,s);}

    public static void Cprint(String s)

   {System.out.print(s);}

    public static double Dinput(String s)

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

   public static int Iinput(String s)

   {     return Integer.parseInt(JOptionPane.showInputDialog(s));}

   public static String input(String s)

   {     return JOptionPane.showInputDialog(s);} 

}

 

W4Ex1  for

 class f1

{public int func(int x)

 {return x*x+2*x+1;}

}

public class W4Ex1

{ public static void plot(f1 f,int xmin,int xmax)

  {     int x=xmin;

        String sum="";

        String s="";

        int y=0;

        while(x<=xmax)

        {   y=f.func(x);  // function to be plotted

            int i=0;

            sum="*";

            while(i<y)

            { sum = " "+sum;i++;}

            s+=sum+"\n";

            x++;

        }

        IO.print(s);

 }

  public static void main(String arg[])

  { f1 f=new f1();

    plot(f,-10,10);

  }

}

 

public class W4Ex1A

{ public void plot(f1 f,int xmin,int xmax)

  {     int x=xmin;

        String sum="";

        String s="";

        int y=0;

        while(x<=xmax)

        {   y=f.func(x);  // function to be plotted

            int i=0;

            sum="*";

            while(i<y)

            { sum = " "+sum;i++;}

            s+=sum+"\n";

            x++;

        }

        IO.print(s);

 }

  public static void main(String arg[])

  { f1 f=new f1();

    W4Ex1A w=new W4Ex1A();

    w.plot(f,-10,10);

  }

}

 

public class W4Ex1B

{ public static void main(String arg[])

  { f1 f=new f1();

     W4Ex1A xx=new W4Ex1A();

     xx.plot(f,-10,10);

  }

}

 

EX2

book.java

 public class book

{ public String name,author;

  int year;

  public book(String namei,String authori,int yeari)

  {name=namei;author=authori;year=yeari;}

  public String toString()

  {String s="book name = "+name+" book author = "+author+" publication year = "+year+"\n";

   return s;

  }

}

 

W4Ex2.java

public class W4Ex2

{

  public static void main(String arg[])

  { book b1=new book("heat transfer","Özışık",1976);

    book b2=new book("the plaque","Camus",1987);

    IO.print(""+b1+b2);

  }

}

 

 

EX3

rectangle.java

public class rectangle

{ double width,length;

 public rectangle(double widthi,double lengthi)

 {width=widthi;length=lengthi;}

 public double area()

 {return width*length;}

  public String toString()

  {String s="width = "+width+" m length = "+length+" m area = "+area()+" m"+'\u00B2'+"\n";

   return s;

  }}

 

W4Ex3.java

public class W4Ex3

{

  public static void main(String arg[])

  { rectangle k1=new rectangle(1.25,2.0);

    rectangle k2=new rectangle(1.5,2.5);

    IO.print(""+k1+k2);

  }

}

 

 

EX4

public class f2

{public double A;

 public f2(double Ai)

 {A=Ai;}

 public double func(double x,double y)

 {double z=A*x+2.35*y;

  return z;}

}

 

public class W4Ex4

{

  public static void main(String arg[])

  { f2 z1=new f2(2.5);

    f2 z2=new f2(3.0);

    double x=1.2;

    double y=2.2;

    String s="object z1 x = "+x+" y = "+y+" z1(x,y) = "+z1.func(x,y)+" A = "+z1.A+"\n";

    s+="object z2 x = "+x+" y = "+y+" z2(x,y) = "+z2.func(x,y)+" A = "+z2.A+"\n";

    IO.print(s);

  }

}

 

 

EX5

Box.java

public class box

{ double width,length,height;

  String bcolor;

 public box(double widthi,double lengthi,double heighti,String bc)

 {width=widthi;length=lengthi;height=heighti;bcolor=bc;}

 public double area()

 {return width*length+width*height+length*height;}

 public double volume()

 {return length*width*height;}

  public String toString()

  {String s="width = "+width+" m length = "+length+" m height = "+height+" m area = "+area()+" m"+'\u00B2'+" volume = "+volume()+" m"+'\u00B3'+" color = "+bcolor+"\n";

   return s;

  }

}

 

public class W4Ex5

{

  public static void main(String arg[])

  { box b1=new box(1.25,2.0,3.0,"red");

    box b2=new box(1.5,2.5,3.0,"blue");

    IO.print(""+b1+b2);

  }}

 

EX 6

class f3

{public double func(double x)

 {return x*x+2*x+1;}

 public String toString(double x1,double x2,double dx)

 {String s="";

 double eps=0.000001;

  for(double x=x1;x<x2+eps;x+=dx)

  {s+="x="+x+"y="+func(x)+"\n";}

 return s;

 }

}

 

public class W4Ex6

{

  public static void main(String arg[])

  { f3 z1=new f3();

    String s="object z1\n";

    s+=z1.toString(0.0,2,0.1);

    IO.print(s);

  }

}

 

 

HOMEWORK EXERCISES

 

W4HW1 : Investigate excersise W4Ex5. Write a sphere class to calculate area and volume of sphere for D=0.1 m ,0.2 m and 0.3 m

public class W4HW1

{ public static void main(String arg[])

  {sphere s1=new sphere(0.1);

   sphere s2=new sphere(0.2);

   sphere s3=new sphere(0.3);

   String s="s1 = "+s1+"s2 = "+s2+"s3 = "+s3;

   IO.print(s);

  }

}

 

W4HW2

Class f3 is given as

class f3

{public double func(double x)

 {return x*x+2*x+1;}

}

 

Call the class and method in main method of program W4HW2.java to calculate function value for x=0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9

(hint you can use for loop to create x values)

class f3

{public double func(double x)

 {return x*x+2*x+1;}

}

 

public class W4HW2

{ public static void main(String arg[])

  { String s="";

    f3 f=new f3();

    for(double x=0.1;x<1.0;x+=0.1)

    {s+="x = "+x+" y = "+f.func(x)+"\n";}

    IO.print(s);

  }

}

 

 

Now call the class f3 to achive the same result, write W4HW2A.java

 

W4HW3

class f4

{public double func(double x,double y)

 {return x*x+2*x*y+y-10;}

 public String toString(double x1,double x2,double dx,double y1,double y2,double dy)

 {String s="";

 double eps=0.000001;

  for(double x=x1;x<x2+eps;x+=dx)

  { for(double y=y1;y<y2+eps;y+=dy)

    {s+="x="+x+"y="+y+"z="+func(x,y)+"\n";}

  }

 return s;

 }

}

Class f4 is given to calculate function value for x=0.0,0.25,0.5,0.75,1.0 and y=0.0,0.25,0.5,0.75,1.0 write class W4HW3