(Java) Below are three arrays each containing 50 values. Store all three arrays in the main method. Write an object to transfer the three arrays.
int []num_wallet = {4, 3, 4, 2, 4, 6, 7, 2, 6, 1, 6, 1,1,5, 1, 4, 5, 4, 3, 2, 1, 2, 3, 3, 2, 3, 6, 5, 0, 0, 1, 3, 1, 5, 1, 0, 3, 0, 2, 1, 2, 3, 4, 1, 2, 0, 0, 1, 4, 1, 5};
int []dollar[] = {40, 50, 50, 36, 78, 32, 45, 20, 10, 45, 30, 34, 64, 70, 65, 24, 60, 69, 78, 90, 45, 20, 45, 70, 23, 54, 60, 32, 46, 23, 67, 30, 65, 30, 50, 46, 76, 34, 13, 40, 60, 36, 75, 52, 20, 23, 37, 25, 30, 20};
int []weekly_pay = {1501, 556, 540, 2440, 2240, 2451, 1430, 1730, 1801, 1501, 1301, 2510, 2440, 1450, 2700, 2550, 1000, 850, 400, 600, 450, 1300, 2050, 1000, 2250, 450, 1900, 800, 2500, 1000, 1200, 670, 1100, 1000, 450, 1000, 750, 2200, 2600, 1000, 2150, 450, 1250, 670, 800, 670, 2150, 1000, 2350, 670};
1) In the class file, write a method called dollaroption to compute the average dollar amount.
2) In the class file, write a method called walletoption to compute the average dollar amount of those who have exactly 3 wallets.
3) In the class file, write a method called payoption to compute the lowest dollar amount of those who have a weekly pay of $1000 to $2000
Solution :
NOTE : If you have any problem regarding code, kindly let me know in the comment box, i will do the needful
//class to perform required operation
class Calc{
int []num_wallet;
int []dollar;
int []weekly_pay;
//constructor to assign values passed in main method
Calc(int []num_wallet, int []dollar, int []weekly_pay){
this.num_wallet=num_wallet;
this.dollar = dollar;
this.weekly_pay = weekly_pay;
}
//find the avg. dollar amount
public void dollarOption(){
double total_amt = 0.0;
for(int i = 0; i<dollar.length;i++)
total_amt+=dollar[i];
double avg = total_amt/dollar.length;
System.out.println("Average dollar amount is " + avg);
}
//find the average dollar amount of those who have exactly 3 wallets
public void walletOption(){
double total_amt = 0.0;
int count = 0;
for(int i = 0; i<num_wallet.length;i++){
if(num_wallet[i] == 3){
total_amt+=dollar[i];
count++;
}
}
System.out.println("Average dollar amount of those who have exactly 3 wallets is " + total_amt/count);
}
//find the lowest dollar having weekly pay between 1000 and 2000
public void payoption(){
int lowest = 9999;
for(int i = 0;i<weekly_pay.length;i++){
if(weekly_pay[i]>1000 && weekly_pay[i]<2000){
if(dollar[i] < lowest)
lowest = dollar[i];
}
}
System.out.println("The lowest dollar amount of those who have a weekly pay of $1000 to $2000 is " + lowest);
}
}
class Main {
public static void main(String[] args) {
int []num_wallet = {4, 3, 4, 2, 4, 6, 7, 2, 6, 1, 6, 1,1,5, 1, 4, 5, 4, 3, 2, 1, 2, 3, 3, 2, 3, 6, 5, 0, 0, 1, 3, 1, 5, 1, 0, 3, 0, 2, 1, 2, 3, 4, 1, 2, 0, 0, 1, 4, 1, 5};
int []dollar = {40, 50, 50, 36, 78, 32, 45, 20, 10, 45, 30, 34, 64, 70, 65, 24, 60, 69, 78, 90, 45, 20, 45, 70, 23, 54, 60, 32, 46, 23, 67, 30, 65, 30, 50, 46, 76, 34, 13, 40, 60, 36, 75, 52, 20, 23, 37, 25, 30, 20};
int []weekly_pay = {1501, 556, 540, 2440, 2240, 2451, 1430, 1730, 1801, 1501, 1301, 2510, 2440, 1450, 2700, 2550, 1000, 850, 400, 600, 450, 1300, 2050, 1000, 2250, 450, 1900, 800, 2500, 1000, 1200, 670, 1100, 1000, 450, 1000, 750, 2200, 2600, 1000, 2150, 450, 1250, 670, 800, 670, 2150, 1000, 2350, 670};
Calc ob = new Calc(num_wallet, dollar, weekly_pay);
ob.dollarOption();
ob.walletOption();
ob.payoption();
}
}
Output :
Get Answers For Free
Most questions answered within 1 hours.