COMP 2013 Assignment #1 Due:1/30/2018 (before class) Problem: The Sangree old Fo
ID: 3880963 • Letter: C
Question
COMP 2013 Assignment #1 Due:1/30/2018 (before class) Problem: The Sangree old Folks Home is in deep financial trouble and the owner, old Doc's Sangree, decides to go on a massive fundraising campaign. The local high school offers to help by sponsoring a dance marathon. Old Doc Sangree would lik the data gathered on the couples who will dance and their sponsors e you to write a program to process Input: Every couple that dances is given a unique ID number. Each sponsor is asked how much they are willing to donate for each hour the couple dances and the maximum number of hours they are willing to pay for Data: ID HRS Rate ect oent 0.80 2.00 3.40 2.25 1.00 0.65 1.50 4.00 10.00 4.50 3.75 141 161 132 141 141 161 132 157 4.75 a) The first number is the ID number of the couple being sponsored (ID) b) The second number represents the maximum number of hours the sponsor is willing to pay for (HRS) c) The third number is a real value which gives the dollar per hour amount that the sponsor will donate (RATE).
Explanation / Answer
a. Program:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;
public class FundRaising {
public static void main(String[] args) throws Exception {
int i, j, n, temp, temp1, noOfSponsorsPerCouple = 0, coupleId, maxEarnCoupleId = 0, maxEarnNoOfSponsor = 0;
float temp2;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the number of couples");
n = Integer.parseInt(br.readLine());
int id[] = new int[n];
int hrs[] = new int[n];
float amt[] = new float[n];
float totalAmt = 0, totAmtPerCouple = 0, maxEarnTotal = 0;
Set<Integer> maxCouples = new TreeSet<Integer>();
System.out.println("Enter id, max hours and amount per hour");
/* Enter the input values */
for (i = 0; i < n; i++) {
System.out.println("Enter id");
id[i] = Integer.parseInt(br.readLine());
System.out.println("Enter max hour");
hrs[i] = Integer.parseInt(br.readLine());
System.out.println("Enter amount");
amt[i] = Float.parseFloat(br.readLine());
}
for (i = 0; i < n - 1; i++) {
for (j = i + 1; j < n; j++) {
if (id[i] > id[j]) { /* Sorting values in ascending order */
temp = id[i];
id[i] = id[j];
id[j] = temp;
temp1 = hrs[i];
hrs[i] = hrs[j];
hrs[j] = temp1;
temp2 = amt[i];
amt[i] = amt[j];
amt[j] = temp2;
} else if ((id[i] == id[j]) && (amt[i] < amt[j])) { /* If id's are same then sort according to amount in descending order
* We can even join this if and else if statements as
* if ((id[i] > id[j]) || ((id[i] == id[j]) && (amt[i] < amt[j])))
*/
temp = id[i];
id[i] = id[j];
id[j] = temp;
temp1 = hrs[i];
hrs[i] = hrs[j];
hrs[j] = temp1;
temp2 = amt[i];
amt[i] = amt[j];
amt[j] = temp2;
}
}
}
/* Printing the sorted values */
System.out.println("Sorted values.............");
System.out.println("--------------------------------------------------------------------------------------");
for (i = 0; i < n; i++) {
System.out.println("id = " + id[i] + " , hours = " + hrs[i] + " , amount = " + amt[i] + "$");
}
System.out.println();
/*
* Calculating total number of sponsors, total number of couples and the
* total amount in dollars collected in the campaign. Here the rows with
* same id are treated as same couple
*/
System.out.println("Total count of all couples........");
System.out.println("--------------------------------------------------------------------------------------");
for (i = 0; i < n; i++) {
maxCouples.add(id[i]);
totalAmt += amt[i] * hrs[i];
}
System.out.println("Sponsors count = " + n + " , Number of couples = " + maxCouples.size() + " , Total amount collected = " + totalAmt + "$");
System.out.println();
/*
* Calculating the number of sponsors for each couple and the amount
* collected in dollars per couple
*/
System.out.println("Total count per couple basis.......................");
System.out.println("--------------------------------------------------------------------------------------");
Iterator<Integer> io = maxCouples.iterator();
while (io.hasNext()) {
noOfSponsorsPerCouple = 0;
totAmtPerCouple = 0;
coupleId = io.next();
for (i = 0; i < n; i++) {
if (id[i] == coupleId) {
noOfSponsorsPerCouple++;
totAmtPerCouple += amt[i] * hrs[i];
}
}
/*
* Finding the couple who collected the maximum amount in dollars
*/
if (maxEarnTotal < totAmtPerCouple) {
maxEarnTotal = totAmtPerCouple;
maxEarnCoupleId = coupleId;
maxEarnNoOfSponsor = noOfSponsorsPerCouple;
}
System.out.println("Couple id = " + coupleId + " , Number of sponsors for couple = " + noOfSponsorsPerCouple + " , Total Amount per couple = " + totAmtPerCouple + "$");
}
System.out.println();
System.out.println("Max earn couple details..............");
System.out.println("--------------------------------------------------------------------------------------");
System.out.println("Couple id = " + maxEarnCoupleId + " , Number of sponsors = " + maxEarnNoOfSponsor + " , Total amount they collected = " + maxEarnTotal + "$");
}
}
b.Input:
Enter the number of couples
12
Enter id, max hours and amount per hour
Enter id
117
Enter max hour
5
Enter amount
0.80
Enter id
141
Enter max hour
2
Enter amount
2.00
Enter id
161
Enter max hour
6
Enter amount
3.40
Enter id
117
Enter max hour
3
Enter amount
2.25
Enter id
117
Enter max hour
5
Enter amount
1.00
Enter id
141
Enter max hour
5
Enter amount
0.65
Enter id
132
Enter max hour
3
Enter amount
1.50
Enter id
141
Enter max hour
2
Enter amount
4.00
Enter id
141
Enter max hour
1
Enter amount
10.00
Enter id
161
Enter max hour
4
Enter amount
4.50
Enter id
132
Enter max hour
3
Enter amount
3.75
Enter id
157
Enter max hour
4
Enter amount
4.75
c.Output:
Sorted values.............
--------------------------------------------------------------------------------------
id = 117 , hours = 3 , amount = 2.25$
id = 117 , hours = 5 , amount = 1.0$
id = 117 , hours = 5 , amount = 0.8$
id = 132 , hours = 3 , amount = 3.75$
id = 132 , hours = 3 , amount = 1.5$
id = 141 , hours = 1 , amount = 10.0$
id = 141 , hours = 2 , amount = 4.0$
id = 141 , hours = 2 , amount = 2.0$
id = 141 , hours = 5 , amount = 0.65$
id = 157 , hours = 4 , amount = 4.75$
id = 161 , hours = 4 , amount = 4.5$
id = 161 , hours = 6 , amount = 3.4$
Total count of all couples........
--------------------------------------------------------------------------------------
Sponsors count = 12 , Number of couples = 5 , Total amount collected = 114.15$
Total count per couple basis.......................
--------------------------------------------------------------------------------------
Couple id = 117 , Number of sponsors for couple = 3 , Total Amount per couple = 15.75$
Couple id = 132 , Number of sponsors for couple = 2 , Total Amount per couple = 15.75$
Couple id = 141 , Number of sponsors for couple = 4 , Total Amount per couple = 25.25$
Couple id = 157 , Number of sponsors for couple = 1 , Total Amount per couple = 19.0$
Couple id = 161 , Number of sponsors for couple = 2 , Total Amount per couple = 38.4$
Max earn couple details..............
--------------------------------------------------------------------------------------
Couple id = 161 , Number of sponsors = 2 , Total amount they collected = 38.4$
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.