W5 COMPUTER PROGRAMMINING 2020 SPRING

W5 Array structures of basic variable types

 

 

W5Ex1   

 class W5Ex1

{

public static void main(String args[] )

{

int days_of_the_month[];

days_of_the_month=new int[12];

days_of_the_month[0]=31;

days_of_the_month[1]=28;

days_of_the_month[2]=31;

days_of_the_month[3]=30;

days_of_the_month[4]=31;

days_of_the_month[5]=30;

days_of_the_month[6]=31;

days_of_the_month[7]=31;

days_of_the_month[8]=30;

days_of_the_month[9]=31;

days_of_the_month[10]=30;

days_of_the_month[11]=31;

String s="Month April is "+days_of_the_month[3]+" days long";

System.out.println(s);

}}

 

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

W5Ex2.java

 public class W5Ex2

 {

 public static int[] multiply(int a[],int b[])

 {   int c[]=new int[a.length];

             if(a.length!=b.length) {System.out.println("error wrong vector size!");}

             else

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

                  {c[i]=a[i]*b[i];}

             }

             return c;        

 }

 public static void main(String[] args)

 {

     int a1[]={1,2,3,4,5,6};

     int a2[]={1,4,9,16,25,36};

     int a3[]=multiply(a1,a2);

     String s="array1 output : ";

     String s1="a1 =\n"+output(a1,5);

     s1+="a2 = \n"+output(a2,5);

     s1+="a3 = a1*a2 \n"+output(a3,5);

     System.out.println(s1);

}

 public static String output(int a[],int n)

 {

 String s1="[";

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

    { s1+=String.format("%"+n+"d",a[i]);}

   s1+="]\n";

   return s1;

 }}

 

W5Ex2A.java

import javax.swing.JOptionPane;

 import java.util.*;

 public class W5Ex2A

 {

 public static double[] multiply(double a[],double b[])

 {   double c[]=new double[a.length];

             if(a.length!=b.length) {System.out.println("error wrong vector size!");}

             else

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

                  {c[i]=a[i]*b[i];}

             }

             return c;        

 }

 public static double[] input(int n)

 {  double a[]=new double[n];

 String s="";

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

    {s=JOptionPane.showInputDialog("a["+i+"] = ?");

     a[i]=Double.parseDouble(s);

    }

    return a;

 }

 public static String output(double a[],int n)

 { Locale us=new Locale("us");

 String s1="[";

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

    { s1+=String.format(us,"%"+n+"f  ",a[i]);}

   s1+="]\n";

   return s1;

 }

 public static void main(String[] args)

 {  double a1[]=input(6);

    double a2[]=input(6);

    double a3[]=multiply(a1,a2);

     String s="array1 output : ";

     String s1="a1 =\n"+output(a1,5);

     s1+="a2 = \n"+output(a2,5);

     s1+="a3 = a1*a2 \n"+output(a3,5);

     JOptionPane.showMessageDialog(null,s1);

}

}

Format Specifiers

Here is a quick reference to all the conversion specifiers supported.

SPECIFIER

APPLIES TO

OUTPUT

%a

floating point (except BigDecimal)

Hex output of floating point number

%b

Any type

“true” if non-null, “false” if null

%c

character

Unicode character

%d

integer (incl. byte, short, int, long, bigint)

Decimal Integer

%e

floating point

decimal number in scientific notation

%f

floating point

decimal number

%g

floating point

decimal number, possibly in scientific notation depending on the precision and value.

%h

any type

Hex String of value from hashCode() method.

 %n

none

Platform-specific line separator.

%o

integer (incl. byte, short, int, long, bigint)

Octal number

%s

any type

String value

%t

Date/Time (incl. long, Calendar, Date and TemporalAccessor)

%t is the prefix for Date/Time conversions. More formatting flags are needed after this. See Date/Time conversion below.

%x

integer (incl. byte, short, int, long, bigint)

Hex string.

 

 

EX3

W5Ex3.java

public class W5Ex3

{

//array diecount[0] to diecount[5]

public static int[] die(int n)

{int diecount[]=new int[6];

int die=0;

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

{die=1+(int)(6*Math.random());

 diecount[die-1]++;

}

return diecount;

}

public static void main(String args[] )

{ String s=W5Ex2.output(die(6000000),10);

  System.out.println(s);

}}

 

W5Ex3A.java

import javax.swing.*;

 import java.util.*;

public class W5Ex3A

{

//array diecount[0] to diecount[5]

public static double[] die(int n)

{double diecount[]=new double[6];

int die=0;

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

{die=1+(int)(6*Math.random());

 diecount[die-1]++;

}

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

{diecount[i]/=n;}

return diecount;

}

 public static String output(double a[],int n)

 { Locale us=new Locale("us");

 String s1="[";

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

    { s1+=String.format(us,"%"+n+"f",a[i]);}

   s1+="]\n";

   return s1;

 }

public static void main(String args[] )

{ String s=output(die(6000000),10);

  JOptionPane.showMessageDialog(null,s);

}}

 

W5Ex3B.java

import javax.swing.*;

public class W5Ex3B

{ public static void main(String args[] )

  { String s=W5Ex3A.output(W5Ex3A.die(6000000),10);

  JOptionPane.showMessageDialog(null,s);

  }

}

 

EX4

public class W5Ex4

{

public static double average(double x[])

{double total=0;

int n=x.length;

int i;

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

{total+=x[i];}

return total/n;

}

public static void main(String args[] )

{ double x[]={1.2,2.3,3.3,2.1,1.2,3.4,5.6,7.8,1.2,3.2};

  IO.print("average = "+average(x));

}}

 

import javax.swing.*;

public class W5Ex4A

{

public static double average(double x[])

{double total=0;

int n=x.length;

int i;

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

{total+=x[i];}

return total/n;

}

public static double[] input()

 {  String s=JOptionPane.showInputDialog("number of data points n = ?");

    int n=Integer.parseInt(s);

                 double a[]=new double[n];

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

    {s=JOptionPane.showInputDialog("a["+i+"] = ?");

     a[i]=Double.parseDouble(s);

    }

    return a;

 }

public static void main(String args[] )

{ double x[]=input();

  System.out.println("average = "+average(x));

}}

 

import javax.swing.*;

public class W5Ex4B

{

public static void main(String args[] )

{ double x[]=W5Ex4A.input();

  System.out.println("average = "+W5Ex4A.average(x));

}}

 

EX5

W5Ex5.java

 import javax.swing.*;

public class W5Ex5

{

public static double standard_deviation(double x[])

{double total=0;

double xavg=W5Ex4A.average(x);

int n=x.length;

int i;

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

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

return Math.sqrt(total/(n-1));

}

public static double[] input()

 {  String s=JOptionPane.showInputDialog("number of data points n = ?");

    int n=Integer.parseInt(s);

                 double a[]=new double[n];

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

    {s=JOptionPane.showInputDialog("a["+i+"] = ?");

     a[i]=Double.parseDouble(s);

    }

    return a;

 }

public static void main(String args[] )

{ double x[]=input();

  System.out.println("average = "+W5Ex4A.average(x)+"\nstandard deviation ="+standard_deviation(x));

}}

W5Ex5A.java

 import javax.swing.*;

public class W5Ex5A

{

public static void main(String args[] )

{ double x[]=W5Ex5.input();

  System.out.println("average = "+W5Ex4A.average(x)+"\nstandard deviation ="+W5Ex5.standard_deviation(x));

}}

 

HOMEWORK EXERCISES

 

W5HW1

 

class W5HW1

{ public static void plot(int z[])

  {     int x=0;

        String sum="";

        String s="";

        int y=0;

        for(int j=0;j<z.length;j++)

        {   y=z[j];  // 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[])

 {

                 

 }

}

 

int i[]={9,4,1,0,1,4,9,16,25} is given plot the array by using * so that plot shape

         *

    *

 *

*

 *

    *

         *

                *

                         *

 

Will come as an output

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)

 

 

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

 

W5HW2 Factorial method is given below. Calculate factorial of integer numbers 1 to 10

public static long factorial(int x)

{ 

if( x <= 1 ) 

return 1;

else

return x * factorial( x - 1); 

}