Exercise: Using TWO classes

Create a program that will allow the user to input his desired number of records to store with the use of arrays.

example:

Enter Number of records: 1

Name Course Year
Ethel Grace Bobis                                   B.S Computer Science1


Let's start with our the first class. Let's name it Sample2.


class Sample2
{
String Arr1[];
String Arr2[];
String Arr3[];
int nItems;

Sample2(int size)
{
Arr1 = new String[size];
Arr2 = new String[size];
Arr3 = new String[size];
nItems=0;
}

void insert (String val)
{
Arr1[nItems] = val;
Arr2[nItems] = val;
Arr3[nItems] = val;
nItems++;
}

void display ()
{
System.out.println("\t **** \t");
System.out.println("The values are: ");
System.out.println("Name" + "\t\t" + "Course" + "\t\t" + "Year");

for (int x = 0; x <= nItems;x++) { System.out.println(Arr1[x] + "\t\t" + Arr2[x] + "\t\t" + Arr3[x]); } } }



And with the second class, let's name it MClass2


import java.io.*;
public class MClass2
{
public static void main (String args[]) throws IOException
{
BufferedReader m = new BufferedReader( new InputStreamReader(System.in));

System.out.println("Enter number of Records: ");
int rec = Integer.parseInt(m.readLine());

Sample2 y = new Sample2 (rec);

for (int x=1; x<= rec; x++) { { System.out.println("Enter Name"); String name = m.readLine(); y.insert(name); } { System.out.println("Enter Course: "); String course = m.readLine(); y.insert(course); } { System.out.println("Enter Year: "); String year = m.readLine(); y.insert(year); } } y.display(); } }


What's the output? The same above isn't it? ;)

3 comments:

  1. wow your a good programmer. That was my waterloo when i was taking advance diploma in computer science in informatics. maybe because i'm old already for this programming thingy... :) god bless you my dear!

    ReplyDelete
  2. programming requires much patience. But i admit i tend to lose it sometimes. haha

    Thank you and Merry Christmas once again. ;)

    ReplyDelete
  3. char..keep up the good work maybe we dont know someday u will follow the steps of Mark Zuckerberg...

    ReplyDelete

Extended Search

Custom Search
Hello World