Thursday, July 22, 2010

2 d or Two-Dimensional arrays in BlueJ


This is continuation of BlueJ array.  In the last posting  two dimensional array on BlueJ was discussed with some BlueJ sample programs. Now we’ll see another program as follows:
  
The annual examination results of 10 students are tabulated as follows
   Roll No.    sub1    sub2    sub3
   --------------------------------
Write a program to read data and determine the following
1. Total marks obtain by each student
2. The highest marks in each subject with Roll number
3. The student who obtained the highest total marks

import java.io.*;
class Student

{
 int result[][]=new int[10][5];
int i,j,k,total,maxr,maxm;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
public void takeData() throws IOException
{

  for(i=0;i< 10;i++)
 {
 total=0;
 for(j=0;j< 4;j++)
 {
 if(j==0)
System.out.println("Enter Roll number\t");
 else if(j==1)
System.out.println ("Enter 1st subject marks\t");
 else if(j==2)
System.out.println ("Enter 2nd subject marks\t");
 else if(j==3)
System.out.println ("Enter 3rd subject marks\t");
result[i][j]=Integer.parseInt(br.readLine());
if(j!=0)
 total+=result[i][j];
 }
 result[i][j]=total;
 }
 }
public void showResult()
{
 System.out.println ("Mark sheet");
System.out.println ("\nRoll       1st     2nd       3rd       Total");
 for(i=0;i< 10;i++)
 {
  for(j=0;j< 5;j++)
  {
System.out.println ("         "+result[i][j]);
  }
System.out.println ();
}
System.out.println (“\n\n”);
System.out.println ("Roll number with total");
printf("\nRoll        Total");
 for(i=0;i< 10;i++)
 {
  for(j=0;j< 5;j++)
  {
  if((j==0)||(j==4))
System.out.println ("       “+result[i][j]);
  }
System.out.println ();
}
 for(i=1;i< 4;i++)
 {
  for(j=0;j< 10;j++)
   {
    if(j==0)
    {
    maxm=result[j][i];
    maxr=result[j][0];
    }
    else if(result[j][i]>maxm)
    {
    maxm=result[j][i];
    maxr=result[j][0];
    }
   }
   if(i==1)
System.out.println ("\nMaximum marks in sub1 is :”+maxm + “   “+maxr);
   else if(i==2)
System.out.println ("\nMaximum marks in sub2 is :”+maxm + “   “+maxr);
   else if(i==3)
   System.out.println ("\nMaximum marks in sub3 is :”+maxm + “   “+maxr);
   }
 for(i=0;i< 10;i++)
 {
  for(j=0;j< 5;j++)
  {
   if(j==4)
   {
   if(i==0)
   {
   maxm=result[i][j];
   maxr=result[i][0];
   }
   else if(result[i][j]>maxm)
   {
    maxm=result[i][j];
    maxr=result[i][0];
    }
   }
  }
 }
System.out.println ("\nMaximum total is :”+maxm + “   “+maxr);
}
}


In this Bluej array program we’ll  display a 5 by 5 array with the following output
Upper left traingle with +1
Lower left traingle with -1
Right to left diagonal with 0

class Arr
 {
int result[][]=new int[5][5];
int i,j;
public void show()
{
   for(i=0;i< 5;i++)
 {
 for(j=0;j< 5;j++)
 {
  if(i+j==4)
  result[i][j]=0;
  else if(i+j<4)
  result[i][j]=1;
  else
  result[i][j]=-1;
  }
 }
  for(i=0;i< 5;i++)
 {
 for(j=0;j< 5;j++)
 {
  System.out.print("    "+result[i][j]);
  }
    System.out.print ();
  }
}
}

4 comments:

  1. Sir, i know nothing about bluejava, i want to learn it from the begining. How can i start?

    ReplyDelete
  2. Ok. within 2-3 days, i'll post one page completely for beginners.

    ReplyDelete
  3. Hello! How do you modify elements in an array that has been set as a private instance field?

    ReplyDelete
  4. Private members of an object are accessible by it's functions. So there is no problem in modifying them. Only final members can not be modified.

    ReplyDelete

Subscribe via email

Enter your email address:

Delivered by FeedBurner