In this project, you will write a Java program that randomly
generates annual salaries for professors
in the Computer Science and Engineering Technology (CSET) program
at the University of Toledo
in the past 10 years. Assume that there are five CSET professors.
You should have a twodimensional array of values representing
salaries. The first dimension represents the professors
and the second dimension represents the years of the salaries. You
should have a constructor that
takes two integers representing the number of professors and the
number of years, then randomly
generate the salaries and fill the array.
You should include the following methods.
✓ A method returning the index of the professor that made the least
salary over the 10 years
✓ A method returning the average salary made by all the professors
over the 10 years
✓ A method returning the year when the lowest salary was
earned
✓ A method returning the total amount of money made by all the
professors over the 10 years
Deliverables
1) Clearly written and well-documented java code. The code must
provide a description of what
the code accomplishes. Comments must be provided in the code as
needed. (20 Points).
2) A technical project report that outlines the processes involved
in writing the code, how the
concepts learned in class were helpful in the project, clear
milestones from start to finish of the
project, challenges and how you overcame them. (5 Points)
Grading Criteria
1) Does the code achieve the stated objective accurately and
without any error?
2) Does the code have well-written comments to guide the
reader?
3) Does the code flow logically from one point to the next
4) Is the code lean, clean, and elegant?
6) Are the variables well defined?
7) Does the code follow the java naming convention?
The following is the code for above question. This code has met all the requirements that are stated in the question.
import java.util.*;
public class Main // java class with name Main
{ private static int professors, years; //Declare the variables professors and years as private static of type int. Declaring static will help us to access them in static methods.
private static int[][] salary; //Two-Dimensional array to store salaries of professors over past years
Main(int professors,int years) //Constructor passing number of professors and years
{
this.professors=professors;
this.years=years;
this.salary=new int[professors][years]; //we will now declare the size of salary 2d array
for(int i=0;i<professors;i++) //Now generate the random salaries when the Constructor is called.
{
for(int j=0;j<years;j++)
{
salary[i][j]=(int)(Math.random() * (50000 - 35000 + 1) + 35000); //Here 50000 is the max value and 35000 is the min value
}
}
}
public static int least_salary() //method to return the index of professor having least salary
{ int min,index=0;
min=salary[0][0]; //we first initialize min with salary[0][0]
for(int i=0;i<professors;i++)
{
for(int j=0;j<years;j++) //over the loop
{
if(min>salary[i][j]) // if there is any value which is less tha min, the update the min value with salary[i][j]
{
min=salary[i][j];
index=i+1; //Also update the index value.
}
}
}
return index; //return professor index value
}
public static double average_salary() //function to calculate the average salary
{
double sum=0,avg; //initialize sum=0
for(int i=0;i<professors;i++)
{
for(int j=0;j<years;j++)
{
sum=sum+salary[i][j]; //add salaries of professors to sum
}
}
avg=sum/professors; //Now average_salary will be the sum divide by number of professors
return avg; //return avg
}
public static int lowest_salary_year() //function to calculate the lowest_salary_year
{
int min,year=0;
min=salary[0][0];
for(int i=0;i<professors;i++)
{
for(int j=0;j<years;j++)
{
if(min>salary[i][j]) //if min is > salary, then update the min value. Also the year value
{
min=salary[i][j];
year=j+1; //Now its j not i. Because in our 2d array j represents year.
}
}
}
return year;
}
public static double total_salary() //functiom to calculate total_salary
{
double sum=0;
for(int i=0;i<professors;i++)
{
for(int j=0;j<years;j++)
{
sum=sum+salary[i][j]; //add salary to sum
}
}
return sum;
}
public static void main(String[] args) { //main function
Main professor=new Main(4,5); //create an object professor with number od professors as 4 and years as 5
System.out.println("The index value of professor having least salary is "+professor.least_salary());
System.out.println("The average salary of all the professors is "+professor.average_salary());
System.out.println("The index value of year having lowest salary is "+professor.lowest_salary_year());
System.out.println("The total salary of all the professors is "+professor.total_salary());
}
}
The above code will create a 2-dimensional array of type int to store salaries of professors over the years. The functions created will help us to calculate average salary, professor with least salary, year with lowest salary and sum of the salaries of all the professors which are randomly generated.
I am also attaching the output for your reference.
Output:
The index value of professor having least salary is 1
The average salary of all the professors is 218086.5
The index value of year having lowest salary is 4
The total salary of all the professors is 872346.0
#Please dont forget to upvote if you find the solution helpful. Feel free to ask doubts if any, in the comments section. Thank you.
Get Answers For Free
Most questions answered within 1 hours.