In this lab, you are introduced to two-dimensional arrays. You are provided with
ID: 3803458 • Letter: I
Question
In this lab, you are introduced to two-dimensional arrays. You are provided with the class Person, and five one-dimensional arrays of Person, and required to combine them into a two-dimensional array The data represents a list of appointments for five days of the week. Each array contains the people scheduled for one day's appointment. Follow the directions exactly, and in order Task #1 Find an element of a 1D array l. Using the existing ID array called tuesdayAppts, find Person p7 2. Set intToPrint as the ID of Person p7, using tuesdayAppts and the accessor found in the file Person java. Do not access p7 directly 3. Set fNameToPrint and lNameToPrint of Person p7 4. Run the program to see the result. Task #2 Create a 2D Array l. Declare a2D array called weeklyAppts 2. Instantiate weeklyAppts using the five daily appointment ID arrays. Task #3 Find an element of a 2D array l. Using the 2D array called weeklyAppts, find Person #8 (p8). 2. Set intToPrint as the ID of Person p8, using weeklyAppts. 3. Set fName ToPrint and lNameToPrint of Person p8. 4. Run the program to see the result. Task #4 Find the length of a 1D array in a 2D array l. Using weeklyAppts, set int ToPrint to the length of thursdayAppts 2. Run the program to see the result.Explanation / Answer
package myProject;
import java.text.*; // to use Decimal Format
//Class Person definition
class Person
{
//Instance variable declaration
String fName;
String lName;
int ID;
static int nextID = 101;
//Default Constructor
Person()
{
fName = "unk";
lName = "unk";
ID = nextID++;
}//End of constructor
//Parameterized constructor
Person(String fName, String lName)
{
this.fName = fName;
this.lName = lName;
ID = nextID++;
}//End of constructor
//Sets the first name
public void setFName(String fName)
{
this.fName = fName;
}//End of method
//Returns first name
public String getFName()
{
return fName;
}//End of method
//Sets last name
public void setLName(String lName)
{
this.lName = lName;
}//End of method
//Returns last name
public String getLName()
{
return lName;
}//End of method
//Returns ID
public int getID()
{
return ID;
}//End of method
}//End of class
//Driver class
public class TwoD_ArrayDriver
{
//Main method
public static void main(String[] args)
{
DecimalFormat myFormat;
// to get 2 decimals every time
myFormat = new DecimalFormat("#.00");
//Creates Person objects
Person p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15, p16, p17, p18, p19, p20, p21;
//Initializes Person Objects
p1 = new Person("Abby", "Arthur"); p2 = new Person("Bubba","Brown"); p3 = new Person("Chuckie", "Cheese"); p4 = new Person("Don", "Drysdale"); p5 = new Person("Ernie","Eastwood");
p6 = new Person("Flo", "Fauntroy"); p7 = new Person("Gabby", "Giffords"); p8 = new Person("Hank","Hoover"); p9 = new Person("Indy", "Imhauf"); p10 = new Person("Jim","Jones");
p11 = new Person("Ken", "Koopman"); p12 = new Person("Larry", "Lancelot"); p13 = new Person("Michael", "Moore" ); p14 = new Person("Nina","Nonesuch"); p15 = new Person("Oscar","OToole");
p16 = new Person("Pat","Pompous"); p17 = new Person("Quincy","Quinton"); p18 = new Person("Ralph","Rancid"); p19 = new Person("Steven","Simpson"); p20 = new Person("Tim","Tinker");
p21 = new Person("Uncle","Usher");
Person[] allPersons = {p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15, p16, p17, p18, p19, p20, p21};
Person[] mondayAppts = {p1, p2, p3, p4, p5};
Person[] tuesdayAppts = {p6, p7, p8};
Person[] wednesdayAppts = {p9, p10, p11, p12};
Person[] thursdayAppts = {p13, p14, p15, p16, p17};
Person[] fridayAppts = {p18, p19, p20, p21};
//Task #1 Print the id, first name, and last name of Person #7, using tuesdayAppts
int intToPrint = allPersons[6].ID;
String fNameToPrint = allPersons[6].fName;
String lNameToPrint = allPersons[6].lName;
System.out.println("Person #7 "+intToPrint+" "+fNameToPrint+" "+lNameToPrint);
//Task #2 Create a 2D array out of the 1D arrays
Person [][] weeklyAppts = new Person [5][];
weeklyAppts[0] = mondayAppts;
weeklyAppts[1] = tuesdayAppts;
weeklyAppts[2] = wednesdayAppts;
weeklyAppts[3] = thursdayAppts;
weeklyAppts[4] = fridayAppts;
//Task #3 Print the id of Person #8, using weeklyAppts
intToPrint = weeklyAppts[1][2].ID;
fNameToPrint = weeklyAppts[1][2].fName;
lNameToPrint = weeklyAppts[1][2].lName;
System.out.println("Person #8's ID "+intToPrint+" "+fNameToPrint+" "+lNameToPrint);
//Task #4 Print the length of thursdayAppts using weeklyAppts
intToPrint = 0;
System.out.println("Length of Thursday's appts "+intToPrint);
//Task #5 Print all using weeklyAppts
int ctr = 1;
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < weeklyAppts[i].length; j++)
{
intToPrint = weeklyAppts[i][j].ID;
fNameToPrint = weeklyAppts[i][j].fName;
lNameToPrint = weeklyAppts[i][j].lName;
System.out.println("Appt #"+(ctr++)+" "+intToPrint+" "+fNameToPrint+" "+lNameToPrint);
}//End of inner loop
}//End of outer loop
}//End of main
}//End of class
Output:
Person #7 107 Gabby Giffords
Person #8's ID 108 Hank Hoover
Length of Thursday's appts 0
Appt #1 101 Abby Arthur
Appt #2 102 Bubba Brown
Appt #3 103 Chuckie Cheese
Appt #4 104 Don Drysdale
Appt #5 105 Ernie Eastwood
Appt #6 106 Flo Fauntroy
Appt #7 107 Gabby Giffords
Appt #8 108 Hank Hoover
Appt #9 109 Indy Imhauf
Appt #10 110 Jim Jones
Appt #11 111 Ken Koopman
Appt #12 112 Larry Lancelot
Appt #13 113 Michael Moore
Appt #14 114 Nina Nonesuch
Appt #15 115 Oscar OToole
Appt #16 116 Pat Pompous
Appt #17 117 Quincy Quinton
Appt #18 118 Ralph Rancid
Appt #19 119 Steven Simpson
Appt #20 120 Tim Tinker
Appt #21 121 Uncle Usher
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.