link to 1st part of this question: http://www.cramster.com/answers-apr-11/comput
ID: 3625603 • Letter: L
Question
link to 1st part of this question:
http://www.cramster.com/answers-apr-11/computer-science/java-array-part-print-arrays_1286922.aspx
---------------------------------------------------------------------------------------------
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 ascending sequence by name (USE A DIFFERENT SORT ALGORITHM THAN THE ONE YOU USED IN PART II) 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.
----------------------------------------------------------------------------------------------
If someone solve 1st and 2nd part of the problem please help me with 3rd (last) part of the problem by following the link, http://www.cramster.com/answers-apr-11/computer-science/java-array-3rd-part-link-1st-part-questionhttp-wwwcramstercom_1287002.aspx
Explanation / Answer
Here is everything .
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
public class driver {
public static void main(String args[]) {
HashMap map = new HashMap();
HashMap map2 = new HashMap();
String peopleName[] = { "Anne", "Bob", "Ralph", "Tim", "Barbara",
"Jane", "Steve", "Tom", "Mike", "Shirley", "Pam", "Frank" };
int weight[] = { 30, 150, 305, 225, 135, 160, 80, 200, 165, 90, 100,
120 };
for (int i = 0; i < peopleName.length; i++) {
People people = new People(peopleName[i], weight[i]);
System.out.println(people.toString()); // part I Result
map.put(weight[i], peopleName[i]);
map2.put(peopleName[i], weight[i]);
}
inelevators(map, weight); // part 2 Result
Arrays.sort(weight);
Arrange(map, weight); // part 3 result
Arrays.sort(peopleName);
ArrangeByName(map2, peopleName); // part 4 result
}
private static void ArrangeByName(HashMap map,
String[] peopleName) {
int total = 0;
System.out.println("ArrageByName");
for (int i = 0; i < peopleName.length; i++) {
if (total < 1100) {
System.out.println(peopleName[i] + " " + map.get(peopleName[i])
+ ",");
total = total+map.get(peopleName[i]);
}
}
System.out.println("the total is "+ total);
}
private static void Arrange(HashMap map, int[] weight) {
int total = 0;
System.out
.println(" Rearrange these people in ascending sequence by weight and these people are in Elevator: ");
for (int i = 0; i < weight.length; i++) {
if (total < 1100) {
System.out.print(map.get(weight[i]) + " " + weight[i] + ",");
total = total + weight[i];
}
}
System.out.println(" the total weight is" + total);
}
private static void inelevators(HashMap map, int weight[]) {
System.out.println("These people are in elevators");
int total = 0;
for (int i = 0; i < weight.length; i++)
if (total < 1100) {
System.out.print(map.get(weight[i]) + ",");
total = total + weight[i];
}
}
}
public class People {
private String people;
private int weigth;
public People(String people, int weigth){
this.people = people;
this.weigth = weigth;
}
public String getPeople() {
return people;
}
public void setPeople(String people) {
this.people = people;
}
public int getWeigth() {
return weigth;
}
public void setWeigth(int weigth) {
this.weigth = weigth;
}
public String toString(){
return people +" "+ weigth;
}
}
I didnt stict following object-oriented just because it is easier done at map....
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.