COMPUTER PROGRAMMING 2014 WEEK 6

ARRAYS

EXERCISES (EXERCISE problems are for your understanding the topic. Will not return as homework)

GROUP 1 ARRAY OF SIMPLE VARIABLES

import java.io.*;

class array1a

{

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;

System.out.println("Month April is "+days_of_the_month[3]+" days long");

}

}

 

import java.io.*;

class array1b

{

public static void main(String args[] )

{

int days_of_the_month[]={31,28,31,30,31,30,31,31,30,31,30,31};

System.out.println("Month April is "+days_of_the_month[3]+" days long");

}

}

 

import javax.swing.*;

import java.util.Locale;

 

 public class array1c 

 {

 public static void main(String[] args)

 {

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

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

     int a3[]=new int[a2.length];

     String s="array1 output : ";

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

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

     for(int i=0;i<a1.length;i++) {a3[i]=a1[i]*a2[i];}

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

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

}

 public static String output(int a[])

 {

 Locale tr=new Locale("TR");

 int x=25;

 String s1="";

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

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

   s1+="\n";

   return s1;

 }

 

 }

 

import javax.swing.*;

import java.util.Locale;

 

 public class array1c 

 {

 public static void main(String[] args)

 {

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

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

     int a3[]=new int[a2.length];

     String s="array1 output : ";

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

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

     for(int i=0;i<a1.length;i++) {a3[i]=a1[i]*a2[i];}

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

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

}

 public static String output(int a[])

 {

 String s1="[";

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

    { s1+=a[i]+" ";}

   s1+="]\n";

   return s1;

 }

 

 }

 

import javax.swing.*;

import java.util.Locale;

 

 public class array1c_v1 

 {

 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);

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

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

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

}

 public static String output(int a[])

 {

 String s1="[";

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

    { s1+=a[i]+" ";}

   s1+="]\n";

   return s1;

 }

 

 }

 

import javax.swing.*;

class array1d

{

public static void main(String args[] )

{

int array[]={2,7,12,15,18,13,11,7,1};

String grade[]={"FF","FD","DD","DC","CC","CB","BB","BA","AA"};

int i;

String s="";

for(i=0;i<array.length;i++)

{s+=grade[i]+": ";

for(int stars=0;stars<array[i];stars++)

{s+="*";}

s+="\n";

}

JOptionPane.showMessageDialog(null,s);

}

}

 

import javax.swing.*;

class array1e

{

public static String toString(int[] count)

{

String s="";

for(int i=1;i<count.length;i++)

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

return s;          

}

 

public static void main(String args[] )

{

int diecount[]=new int[7];

int die=0;

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

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

 diecount[die]++;

}

JOptionPane.showMessageDialog(null,toString(diecount));

}

}

 

import javax.swing.*;

class array1e_v1

{

// better even if a bit more confusing!

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

public static String toString(int[] count)

{

String s="";

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

{s+=(i+1)+": "+count[i]+"\n";}

return s;          

}

 

public static void main(String args[] )

{

int diecount[]=new int[6];

int die=0;

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

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

 diecount[die-1]++;

}

JOptionPane.showMessageDialog(null,toString(diecount));

}

}

 

import java.io.*;

import javax.swing.*;

class array2 {

public static int month(String s)

{

 

String month[]={"january","february","march","april","may","june","july",

            "august","september","october","november","december"};

int i;

int j=-1;

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

{if(s.equals(month[i])) {j=i;break;}}               

return j;

}

 

             

public static void main(String args[] )

{ int the_day_of_the_month[]={31,28,31,30,31,30,31,31,30,31,30,31};             

String s="The day of the month : \n";

String s2="april";

int i=month(s2);    

s+="Month "+s2+" s "+the_day_of_the_month[i]+" days long \n";

String s1="Array example";

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

}

}

 

class math1

{

public static void main(String args[])

{

int number[]=new number[8];

number[0]=10.0;

number[1]=5.0;

number[2]=7.0;

number[3]=9.0;

number[4]=11.0;

number[5]=13.0;

number[6]=14.0;

number[7]=18.0;

double total=0;

int i;

for(i=0;i<number.length;i++)

{

total+=number[i];

}

System.out.println("Average = "+total/number.length);

}

}

 

class math2

{

public static void main(String args[])

{

int number[]={10,5,7,9,11,13,14,18};

double total=0;

int i;

for(i=0;i<number.length;i++)

{

total+=number[i];

}

System.out.println("Average = "+total/number.length);

}

}

 

GROUP 1 ARRAY OF CLASS OBJECTS

 

class box

{

private double length;

private double width;

private double height;

 

box(double l,double w, double h)

{length=l;

width=w;

height=h;

}

// This methods are written to access private length,

// width and height variables

public double read_length() {return length;}

public double read_width() {return width;}

public double read_height() {return height;}

//This methods are written to change private length,

// width and height variables

public void write_length(double l) {length=l;}

public void write_width(double w) {width=w;}

public void write_height(double h) {height=h;}

 

box(box 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\n";

  return s;

}

 

}

 

import java.io.*;

import javax.swing.*;

class array_box

{

public static void main(String args[] )

{   box x[]=new box[4];

     x[0]=new box(1.1,2.2,3.3);

     x[1]=new box(1.23,2.11,3.12);

     x[2]=new box(2.5,2.11,3.0);

     x[3]=new box(2.5,2.11,3.1);

  String s1="Array example ";

  String s="";

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

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

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

}

}

 

 

import java.util.Locale;

public class vector

 {        

 public double V[];

 //constructor methods

 public vector(double Vi[])

 { V=new double[Vi.length];

   input_vector(Vi);

 }

 

 public vector(vector Vi)

 { V=new double[Vi.V.length];

   input_vector(Vi.V);

 }

 

 public vector(int n)

 { V=new double[n];

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

             { V[i]=0.0;}

 } 

 

 public void input_vector(double Vi[])

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

               {V[i]=Vi[i];}

 }

 

 public static vector add(vector V1,vector V2)

 {

 //

 int n=V1.V.length;

 vector V3=new vector(n);

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

               {V3.V[i]=V1.V[i]+V2.V[i];} 

 return V3;         

 }

 

 public static vector add(double[] V1,double[] V2)

 {

 //

 int n=V1.length;

 vector M=new vector(n);

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

               {M.V[i]=V1[i]+V2[i];} 

 return M;          

 }

 

 public void add(vector V1)

 {

 //        

 int n=V.length;

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

               {V[i]+=V1.V[i];}

 }            

 

 public void add(double [] B)

 {

 //        

 int n=V.length;

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

               {V[i]+=B[i];}               

 }

 

 public static String toString(vector M)

 { int n=M.V.length;

   String s="";

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

               {s+=M.V[i]+" ";}

 s+="\n";

 return s;

 }

 

 public String toString()

 {  Locale tr=new Locale("TR");

            int n=V.length;

    String s1="";

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

    { s1+=String.format(tr," %10.5f ",V[i]);}

   s1+="\n";

 return s1;

 } //       

 

 }//

 

import javax.swing.*;

class array_vector {

public static void main(String args[] )

{ double A[]={1.0,2.0,3.0,4.0};

  double B[]={1.0,1.0,1.0,1.0};

  vector M1=new vector(A);

  vector M2=new vector(B);

  M1.add(M2); 

  String s="summation of two vector : \n"+M1.toString();

  String s1="class vector";

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

} }

 

 

 import java.util.Locale;

public class matrix

 {        

 public double A[][];

 //constructor methods

 public matrix(double Ai[][])

 { A=new double[Ai.length][Ai[0].length];

   input_matrix(Ai);

 }

 

 public matrix(matrix M)

 { A=new double[M.A.length][M.A[0].length];

   input_matrix(M.A);

 }

 

 public matrix(int n,int m)

 { A=new double[n][m];

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

             { for(int j=0;j<m;j++)

               {A[i][j]=0.0;}

     } 

 }

 

 public void input_matrix(double Ai[][])

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

             { for(int j=0;j<A[0].length;j++)

               {A[i][j]=Ai[i][j];}

     }

 }

 

 public static matrix add(matrix M1,matrix M2)

 {

 //

 int n=M1.A.length;

 int m=M1.A[0].length;

 matrix M3=new matrix(n,m);

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

             { for(int j=0;j<m;j++)

               {M3.A[i][j]=M1.A[i][j]+M2.A[i][j];}

     } 

 return M3;        

 }

 

 public static matrix add(double[][] A1,double[][] A2)

 {

 //

 int n=A1.length;

 int m=A1[0].length;

 matrix M=new matrix(n,m);

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

             { for(int j=0;j<m;j++)

               {M.A[i][j]=A1[i][j]+A2[i][j];}

     } 

 return M;          

 }

 

 public void add(matrix M1)

 {

 //        

 int n=A.length;

 int m=A[0].length;

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

             { for(int j=0;j<m;j++)

               {A[i][j]+=M1.A[i][j];}

     }        

 }

 

 public void add(double [][] B)

 {

 //        

 int n=A.length;

 int m=A[0].length;

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

             { for(int j=0;j<m;j++)

               {A[i][j]+=B[i][j];}

     }        

 }

public static double[][] multiply(double[][] left,double[][] right)

{

//multiplication of two matrices

int ii,jj,i,j,k;

int m1=left[0].length;

int n1=left.length;

int m2=right[0].length;

int n2=right.length;

double[][] b;

b=new double[m1][n2];

 if(n1 != m2)

 {

 System.out.println("inner matrix dimensions must agree");

 for(ii=0;ii<n1;ii++)

   {

   for(jj=0;jj<m2;jj++)

     b[ii][jj]=0;

   }

   return b;

 }

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

   {

   for(j=0;j<n2;j++)

     {

     for(k=0;k<n1;k++)

                b[i][j]+=left[i][k]*right[k][j];

     }

   }

return b;

//end of multiply of two matrices

}

 

 public static String toString(matrix M)

 { int n=M.A.length;

   int m=M.A[0].length;

   String s="";

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

             { for(int j=0;j<m;j++)

               {s+=M.A[i][j]+" ";}

             s+="\n";

             }

 return s;

 }

 

 public String toString()

 { 

            int n=A.length;

   int m=A[0].length;

   String s1="";

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

   {  for(int j=0;j<m;j++)

    { s1+=A[i][j]+" ";}

   s1+="\n";

   }

 return s1;

 } //end of method toString                  

 

 }//matrix sınıfının sonu

 

import javax.swing.*;

class array_matrix {

public static void main(String args[] )

{ double A[][]={{1.0,2.0},{3.0,4.0}};

double B[][]={{1.0,1.0},{1.0,1.0}};

matrix M1=new matrix(A);

String s="First Matrix = \n"+M1.toString();

matrix M2=new matrix(B);

s+="Second Matrix = \n"+M1.toString();

M1.add(M2); 

s+="addition of two matrices : \n"+M1.toString();

String s1="Array example: class matrix";

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

} }

 

 

 

 

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 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 array_library

{

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 book("It is all quiet in the westen front ","Erich Maria Reamarque","fiction","English");

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

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

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

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

String s=l.toString();

outputwindow(s);

}

}

 

HOMEWORKS (You will return solutions of this sets as homework)

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

         *

    *

 *

*

 *

    *

         *

                *

                         *

 

In order to establish that use “*” and “ “  (empty space) characters

 

HW 2  A method to multiply two matrices are given in matrix class. use multiply method in matrix class  to multiply two matrices in an example program.

HW 3 create a static method

public static double average(double x[])

{}

To calculate average of a given double array and test the method in a main program

HW 4: Formula for standard deviation is given as

where is the average value write a static method

public static double stdev(double x[])

{}

To calculate standart deviation of a given double array and test the method in a main program