In: Computer Science
Java Programming
Part 1 (20%)
Implement a class with a main method. Using an enhanced for loop, display each element of this array:
String[] names = {"alice", "bob", "carla", "dennis", "earl", "felicia"};
Part 2 (30%)
In a new class, implement two methods that will each calculate and
return the average of an array of numeric values passed into it.
Constraints:
Implement a new class demonstrating your methods in action. Call your methods at least twice each with arrays of different sizes each time.
Part 3 (50%)
The Cool Ice Cream Shoppe pays its summer employees bonuses based
on two factors: the number of weeks worked over the summer, and the
number of positive customer reviews. The table below shows the
bonuses based on these two factors.
Positive Reviews (right) Weeks Worked (down) |
0 | 1 | 2 | 3 | 4 or more |
0 | 25 | 45 | 80 | 110 | 150 |
1 | 50 | 60 | 90 | 120 | 180 |
2 | 100 | 125 | 160 | 210 | 265 |
3 | 160 | 190 | 225 | 275 | 340 |
4 | 230 | 270 | 325 | 385 | 450 |
5 | 300 | 360 | 420 | 480 | 600 |
6 or more | 425 | 500 | 600 | 700 | 875 |
Examples:
Write an application that:
Please up vote ,comment if any query . Thanks for question .Be safe .
Program Part1: ***************************Main.java*****************************
public class Main {
public static void main(String[] args) {
//string array declaration
String[] names = {"alice", "bob", "carla", "dennis", "earl",
"felicia"};
for(int i=0;i<names.length;i++)
{
System.out.println(names[i]);//print each element in new line
}
}
}
Output : Part1
Program Part2 :*******************************ArrayTesting.java***********************************
public class ArrayTesting {
//this function takes integer array and size
public double getAverage(int []array,int size)
{
double sum=0.0; //sum store in this
for(int i=0;i<size;i++)
{
sum+=array[i]; //add each element into sum
}
sum=sum/size; //get average
sum=Math.round(sum * 100.0) / 100.0; //convert it into 2 decimal
point
return sum; //return average
}
//takes double array and size
public double getAverage(double []array,int size)
{
double sum=0.0;
for(int i=0;i<size;i++)
{
sum+=array[i]; //add
}
sum=sum/size; //get average
sum=Math.round(sum * 100.0) / 100.0; //round for 2 decimal
point
return sum; //return average
}
}
Program Part2 : Main class testing
*******************Main.java***************************
public class Main {
public static void main(String[] args) {
int []intArray={3,4,5,7,8,9,11,71}; //integer array of size 8
double
[]doubleArray={3.134,4.2444,5.3458,7.4345,8.545,9.64350,11.7345,71.819};
int size =8; //size declaration
ArrayTesting Obj=new ArrayTesting(); //object create
System.out.println(Obj.getAverage(intArray, size)); //print
average
System.out.println(Obj.getAverage(doubleArray, size));//print
average of double array
}
}
Output :Part2
Program Part3 : **********************Main.java********************************
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in); //Input scanner object
int totalBonus=0; //total bonus
char name='A'; //employee name
int [][]bonusArray={{25,45,80,110,150}, //2d array for bonus
{50,60,90,120,180},
{100,125,160,210,265},
{160,190,225,275,340},
{230,270,325,385,450},
{300,360,420,480,600},
{425,500,600,700,875}};
while(true) //infinite loop
{
int totalWeeks;
int review;
System.out.print("Enter number of weeks employee worked : ");
//prompt for total weeks
totalWeeks=sc.nextInt();
if(totalWeeks==-1) //if weeks -1
{
break; //exit loop and print total bonus
}
else
{
System.out.print("Enter customer reviews : "); //enter review
review=sc.nextInt();
if(review==-1) //if -1 exit program
{
break;
}
else
{
if(review>=4)//if enter 4 or more set 4
review=4;
if(totalWeeks>=6) //if 6 ot more 6
totalWeeks=6;
int bonus=bonusArray[totalWeeks][review]; //get bonus in
bonus
//print bonus of each employee
System.out.println("Employee "+name+" worked "+totalWeeks+" weeks
and got "+review+" positive reviews; bonus:$"+bonus);
totalBonus+=bonus; //get total bonus
name+=1; //increment A to B
}
}
}
//print exit using break print total bonus
System.out.println("Total bonus paid: $"+totalBonus);
}
}
Output : Part3
Please up vote ,comment if any query . Be safe .