Part I: Create and print out the two arrays : (Be sure to do this first) You are
ID: 3767348 • Letter: P
Question
Part I:
Create and print out the two arrays: (Be sure to do this first) You are allowed to hard code these arrays into your program. The data is as follows:
Anne 130
Bob 150
Ralph 305
Tim 225
Barbara 135
Jane 160
Steve 85
Tom 200
Mike 165
Shirley 90
Pam 125
Frank 120
Part II: The elevators in our building have an 1100 lb. load limit. Determine which people in the list above get on the elevator. Print their names, weights, total weight, and how many got on.
Part III: Rearrange these people in ASCENDING sequence by WEIGHT and print the two arrays. Determine again how many may ride the elevator, printing out their names, weights, total weight and the number of how many people got on.
Part IV: Rearrange these people in DECENDING sequence by NAME (USE A DIFFERENT SORT ALGORITHM THAN THE ONE YOU USED IN PART III) and print the two arrays. Determine again how many may ride the elevator, printing out their names, weights, total weight and the number of how many people got on.
Part V: Have the program determine which method allowed the most people to get on the elevator. The program should compare the three different counts of how many people got on the elevator.
This program should include:
2 sort methods
a method to determine how many people get on the elevator (will be called 3 times)
a print method which prints both arrays (include a size parameter and it can be called 6 times).
Make sure all methods are writen to handle n elements, not just 12. You can pass 12 from main to n in the method/function.
Explanation / Answer
import java.util.Scanner;
import java.io.*;
public class Elevator
{
public static void main (String[] args) throws IOException
{
Scanner scan = new Scanner(new File("elevatordata.txt"));
int s = scan.nextInt();
int i = 0;
int count = 0;
int countA;
int countB;
int countC;
PersonList persons = new PersonList();
while(scan.hasNext()){
persons.getPersons(0);
persons.getPersons(1);
persons.getPersons(2);
persons.getPersons(3);
persons.getPersons(4);
persons.getPersons(5);
persons.getPersons(6);
persons.getPersons(7);
persons.getPersons(8);
persons.getPersons(9);
persons.getPersons(10);
persons.getPersons(11);
person.setName(scan.next());
person.setWeight(scan.nextInt());
i++;
int total;
if(total > 1100)
System.out.println("You have exceeded the weight limit!");
countA = persons.howMany;
System.out.println("In the elevator" + countA + "people got on.");
System.out.println("The array by weight is");
countB = persons.sortWeights;
System.out.println("After the people were sorted by weight" + countB + "people got on.");
System.out.println("The array by name is");
countC = persons.sortNames;
System.out.println("After the people were sorted by name" + countC + "people got on.");
if(countA > countB && countA > countC)
{
System.out.println("The first method worked the best");
}
else if(countB > countA && countB > countC)
{
System.out.println("The second method worked the best");
}
else
{
System.out.println("The last method worked the best");
}
My PersonList class is this:
Java Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
public class PersonList
{
private Person[] persons;
private int count = 0;
public PersonList(int size)
{
persons = new Person[size];
persons[0] = new Person ("Anne", 30);
persons[1] = new Person ("Bob", 150);
persons[2] = new Person ("Ralph", 305);
persons[3] = new Person ("Tim", 225);
persons[4] = new Person ("Barbara", 135);
persons[5] = new Person ("Jane", 160);
persons[6] = new Person ("Steve", 80);
persons[7] = new Person ("Tom", 200);
persons[8] = new Person ("Mike", 165);
persons[9] = new Person ("Shirley", 90);
persons[10] = new Person ("Pam", 100);
persons[11] = new Person ("Frank", 120);
count = 0;
}
public void add(Person x)
{
persons[count] = x;
count++;
}
public int howMany()
{
int total = 0, i = 0;
do
{
total += persons[i].getWeight();
System.out.println(" " + (i+1) + ":" + persons[i]);
i++;
}while (total <= 1100);
System.out.println("The total weight is " + (total- (persons[i-1].getWeight())) );
return i-1;
}
public int sortWeights()
{
int weightIndex;
for (int i=0; i < count; i++)
{
weightIndex = i;
for (int j = i+1; j < persons.length; j++)
if (persons[j].getWeight() < persons[weightIndex].getWeight())
weightIndex = j;
Person temp = persons[i];
persons[i] = persons[weightIndex];
persons[weightIndex] = temp;
}
return howMany();
}
public int sortNames()
{
int nameIndex;
for (int i=0; i < count; i++)
{
nameIndex = i;
for (int j = i+1; j < persons.length; j++)
if(persons[i].getName().compareTo(persons[nameIndex].getName())>0)
nameIndex = j;
Person temp = persons[i];
persons[i] = persons[nameIndex];
persons[nameIndex] = temp;
}
return howMany();
}
public String toString()
{
String out ="";
for (int i=0; i<count; i++)
out += (" "+(i+1) + ": " + persons[i]+" ");
return out;
}
}
Then, my Person class is just this:
Java Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import java.util.Scanner;
public class Person
{
private String name;
private int weight;
Scanner scan = new Scanner(System.in);
public Person(String n, int w)
{
name = n;
weight = w;
}
public Person()
{
name = "no name";
weight = 0;
}
public String toString()
{
return name + " " + weight;
}
public String getName()
{
return name;
}
public int getWeight()
{
return weight;
}
public void setName(String n)
{n = name;}
public void setWeight(int w)
{w = weight;}
}
import java.util.Scanner;
import java.io.*;
public class Elevator
{
public static void main (String[] args) throws IOException
{
Scanner scan = new Scanner(new File("elevatordata.txt"));
int s = scan.nextInt();
int i = 0;
int count = 0;
int countA;
int countB;
int countC;
PersonList persons = new PersonList();
while(scan.hasNext()){
persons.getPersons(0);
persons.getPersons(1);
persons.getPersons(2);
persons.getPersons(3);
persons.getPersons(4);
persons.getPersons(5);
persons.getPersons(6);
persons.getPersons(7);
persons.getPersons(8);
persons.getPersons(9);
persons.getPersons(10);
persons.getPersons(11);
person.setName(scan.next());
person.setWeight(scan.nextInt());
i++;
int total;
if(total > 1100)
System.out.println("You have exceeded the weight limit!");
countA = persons.howMany;
System.out.println("In the elevator" + countA + "people got on.");
System.out.println("The array by weight is");
countB = persons.sortWeights;
System.out.println("After the people were sorted by weight" + countB + "people got on.");
System.out.println("The array by name is");
countC = persons.sortNames;
System.out.println("After the people were sorted by name" + countC + "people got on.");
if(countA > countB && countA > countC)
{
System.out.println("The first method worked the best");
}
else if(countB > countA && countB > countC)
{
System.out.println("The second method worked the best");
}
else
{
System.out.println("The last method worked the best");
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.