Jump to content

Featured Replies

Posted

// The "TwoDimensionalArrayTable" class.

//NAME: CLINTON SHEPLEY

//DATE: SEPTEMBER 18

//PROGRAM DESCRIPTION: Two dimensional array are tables with rows and columns

//from page 361 in the java book, adapt the program to use r for the

//first loop and c for the second loop change the nested loop for marks input

//Improve page 361 by adding a single dimension array for names.

 

 

//assign values to the array

import java.awt.*;

import hsa.Console;

 

public class TwoDimensionalArrayTable

{

static Console c; // The output console

 

public static void main (String[] args)

{

c = new Console ();

 

// Place your program here. 'c' is the output console

int rows;

int subj;

c.println ("HOw many people(rows) are in the class?");

rows = c.readInt ();

c.println ("How many subjects are there?");

subj = c.readInt ();

//Variables

String course[];

course = new String [subj]; //array for subjects

String names[];

names = new String [rows]; //array for rows

 

int table[] []; //declare the array

table = new int [rows] [subj]; //**instantiates the array to create a new object

 

for (int cc = 0 ; cc < (subj) ; cc++)

{

c.println ("Enter a subject");

course [cc] = c.readLine ();

 

}

for (int r = 0 ; r < (rows) ; r++)

{

c.println ("Enter a Name");

names [r] = c.readLine ();

 

}

//mainline

for (int r = 0 ; r < (rows) ; r++)

{

// table [r] [0] = r + 1;

c.println ("Enter a Mark for " + names [r]);

for (int cc = 0 ; cc < (subj) ; cc++)

{

c.print ("Enter mark for subject " + course [cc]);

table [r] [cc] = c.readInt (); //initializes and assigns values to the array

}

}

//print table

c.print ("NAME", 7);

for (int cc = 0 ; cc < (subj) ; cc++)

{

c.print (course [cc], 7);

 

 

}

 

c.println ();

 

 

 

for (int r = 0 ; r < (rows) ; r++)

{

c.print (names [r]);

for (int cc = 0 ; cc < (subj) ; cc++)

{

c.print (table [r] [cc], 7);

 

 

}

c.println ();

}

} // main method

} // TwoDimensionalArrayTable class