IN NETBEANS - Java Part 1 Create a program that interviews the users about their
ID: 3769005 • Letter: I
Question
IN NETBEANS - Java
Part 1 Create a program that interviews the users about their personal information, how many jobs they have and what the income is from each of their jobs. The program should then display back to the user the following items:
Their name
How many jobs the user has
What the highest paying job pays
What the lowest paying job pays
What the average for all X number of jobs is
The example shown here represents what the user types in green and what the program displays in red.
What is your name? Jake Lince
How many jobs do you have? 3
Enter income from job #1: 10000
Enter income from job #2: 30000
Enter income from job #3: 50000
Response: Hello, Jake Lince. You have 3 jobs. The highest paying job pays $50000. The lowest paying job pays $10000. The average pay for the 3 jobs entered is $30000.
Part 2 Additionally, your program should write an output file to the location and file name of the user's choosing if they would like an output of the information. (So you should first prompt them to see if they want it output to a data file and then prompt for the data file, if applicable.)
Explanation / Answer
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
/**
*
*/
/**
* @author Srinivas Palli
*
*/
public class PersonalInformation {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
int salary[], noOfJobs, sumOfSalary = 0;
String name;
int highestPay, lowestPay;
String ch = "", fileName = "";
Scanner scanner = new Scanner(System.in);
System.out.print("You want output to a data file(yes/no)? ");
ch = scanner.nextLine();
if (ch.equalsIgnoreCase("yes")) {
System.out.print("Enter the output file name? ");
fileName = scanner.nextLine();
}
System.out.print("What is your name? ");
name = scanner.nextLine();
System.out.print("How many jobs do you have? ");
noOfJobs = scanner.nextInt();
salary = new int[noOfJobs];
for (int i = 0; i < noOfJobs; i++) {
System.out.print("Enter income from job #" + (i + 1) + ": ");
salary[i] = scanner.nextInt();
sumOfSalary += salary[i];
}
highestPay = lowestPay = salary[0];
for (int i = 1; i < noOfJobs; i++) {
if (highestPay < salary[i]) {
highestPay = salary[i];
}
if (lowestPay > salary[i]) {
lowestPay = salary[i];
}
}
String result = "Hello, " + name + ". ";
result += "You have " + noOfJobs + " jobs ";
result += "The highest paying job pays $" + highestPay + " ";
result += "The lowest paying job pays $" + lowestPay + " ";
result += "The average pay for the " + noOfJobs + " jobs entered is $"
+ ((double) sumOfSalary / (double) noOfJobs) + " ";
System.out.println(result);
if (ch.equalsIgnoreCase("yes")) {
File file = new File(fileName);
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(result);
bw.close();
System.out.println("Completed Writing to file...");
}
}
}
OUTPUT:
Test1:
You want output to a data file(yes/no)? no
What is your name? Srinivas Palli
How many jobs do you have? 3
Enter income from job #1: 10000
Enter income from job #2: 20000
Enter income from job #3: 50000
Hello, Srinivas Palli.
You have 3 jobs
The highest paying job pays $50000
The lowest paying job pays $10000
The average pay for the 3 jobs entered is $26666.666666666668
Test2:
You want output to a data file(yes/no)? yes
Enter the output file name? personalinfo.txt
What is your name? Srinivas Palli
How many jobs do you have? 3
Enter income from job #1: 10000
Enter income from job #2: 30000
Enter income from job #3: 50000
Hello, Srinivas Palli.
You have 3 jobs
The highest paying job pays $50000
The lowest paying job pays $10000
The average pay for the 3 jobs entered is $30000.0
Completed Writing to file...
personalinfo.txt
Hello, Srinivas Palli.
You have 3 jobs
The highest paying job pays $50000
The lowest paying job pays $10000
The average pay for the 3 jobs entered is $30000.0