Question

CSC151 JAVA PROGRAMMING LAB #7 OBJECTIVES . . . In this lab assignment, students will learn: To get an overview of exceptions

Part One: Salary Generator Project Description: Write a program named SalaryGenerator.java that can create a data file called

0 0
Add a comment Improve this question Transcribed image text
Answer #1

///////////////////////////////////////////// SalaryGenerator.java ////////////////////////////////////////////

// PLEASE LIKE THE SOLUTION
// FEEL FREE TO DISCUSS IN COMMENT SECTION
// JAVA PROGRAM

import java.io.*;
import java.util.*;

// class
public class SalaryGenerator{
   // main method
   // now opening the file to write in try catch block
   public static void main(String arg[]){
       // try with resources
       try(PrintWriter write = new PrintWriter(new File("Salary.txt"))){
           //now for thousands
           String fn = "FirstName";
           String ln = "LastName";
           String rank[] = {"assistant","associate","full"};
           double salary;

           // now for thousands
           for(int i=0;i<1000;i++){
               write.print((fn+(i+1))+" "+(ln+(i+1))+" ");
               // now rank genearte random 0 to 2
               int r = (int)(Math.random()*3);
               if(r == 0){
                   // generate salary
                   salary = Math.random()*(30001)+50000;
                   // now round up to 2 digits
                   salary = Math.round(salary * 100.0) / 100.0;

                   // now write to file
                   write.print(rank[0]+" "+salary+"\n");
               }

               else if(r==1){
                   // generate salary
                   salary = Math.random()*(50001)+60000;
                   // now round up to 2 digits
                   salary = Math.round(salary * 100.0) / 100.0;

                   // now write to file
                   write.print(rank[1]+" "+salary+"\n");
               }

               else if(r==2){
                   // generate salary
                   salary = Math.random()*(55001)+75000;
                   // now round up to 2 digits
                   salary = Math.round(salary * 100.0) / 100.0;

                   // now write to file
                   write.print(rank[2]+" "+salary+"\n");
               }
           }
          
       }

       catch(Exception e){
           // print the message
           System.out.println(e.getMessage());
       }
   }
}

//////////////////////////////////////////////////////////////////////////////////////////

////////////////////// SAMPLE OUTPUT FILE////////////////////////////////

Salary.txt FirstName1 LastName1 assistant 69527.5 FirstName2 LastName2 assistant 56619.33 FirstName3 LastName3 assistant 7665

/////////////////////////////// SalaryAnalyzer.java /////////////////////////////////////////////////////////////

import java.io.*;
import java.util.*;

// class
public class SalaryAnalyzer{
   //main method
   public static void main(String arg[]){
       // now read all the data and find the
      
       // read the the file data using try with resources
       try(Scanner scan = new Scanner(new File("Salary.txt"))){
           //now open the file
          

           // read the data and store the analyzed data
           int assistantCount = 0;
           int associateCount = 0;
           int fullCount = 0;
           double assistantTotal = 0;
           double associateTotal = 0;
           double fullTotal = 0;

           // now read the data
           String fn;
           String ln;
           String rank;
           double salary;
           while(scan.hasNext()){
               fn = scan.next();
               ln = scan.next();
               rank = scan.next();
               salary = scan.nextDouble();
              
               // now update
               if(rank.equals("assistant")){
                   assistantTotal += salary;
                   assistantCount++;
               }

               else if(rank.equals("associate")){
                   associateTotal += salary;
                   associateCount++;
               }

               else if(rank.equals("full")){
                   fullTotal += salary;
                   fullCount++;
               }

           }

           // now print the result
           System.out.println("Total Salary of Assistant Professors = "+assistantTotal);
           System.out.println("Total Salary of Associate Professors = "+associateTotal);
           System.out.println("Total Salary of Full Professors = "+fullTotal);

           System.out.println("Average Salary of Assistant Professors = "+(assistantTotal/assistantCount));
           System.out.println("Average Salary of Associate Professors = "+(associateTotal/associateCount));
           System.out.println("Average Salary of Full Professors = "+(fullTotal/fullCount));
       }
       catch(Exception e){
           System.out.println(e.getMessage());
       }
   }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//////////// SAMPLE OUTPUT ///////////////////

kashyap@kashyap-Inspiron-3542:-/Pictures/cch$ javac salaryAnalyzer.java kashyap@kashyap-Inspiron-3542:-/Pictures/cch$ java Sa

Add a comment
Know the answer?
Add Answer to:
CSC151 JAVA PROGRAMMING LAB #7 OBJECTIVES . . . In this lab assignment, students will learn:...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Lab Assignment 4B Modify your program in Lab Assignment 4A to throw an exception if the...

    Lab Assignment 4B Modify your program in Lab Assignment 4A to throw an exception if the file does not exist. An error message should result.   Use the same file name in your program as 4A, however, use the new input file in 4B assignment dropbox. Lab 4A: Write a program that reads a file (provided as attachment to this assignment) and writes the file to a different file with line numbers inserted at the beginning of each line. Test with...

  • independent? in 2015, the report on the UC berkley faculty salary equity study shows that 87...

    independent? in 2015, the report on the UC berkley faculty salary equity study shows that 87 of the university's 222 assistant professors were women, along with 137 of the 324 associate professors and 243 of the 972 full professor a. what is the probability that a randomly chosen berkeley professor (of any rank) is a woman? b. What is the conditional probability that a randomly chosen professor is a woman, given that the person chosen is a full professor? c....

  • Last picture is the tester program! In this Assignment, you will create a Student class and...

    Last picture is the tester program! In this Assignment, you will create a Student class and a Faculty class, and assign them as subclasses of the superclass UHPerson. The details of these classes are in the UML diagram below: UHPerson - name : String - id : int + setName(String) : void + getName(): String + setID(int) : void + getID(): int + toString(): String Faculty Student - facultyList : ArrayList<Faculty - rank: String -office Hours : String - studentList...

  • MUST BE IN C++ The lab is called "7.3.7.1 Exceptions: file checks" The lab is as...

    MUST BE IN C++ The lab is called "7.3.7.1 Exceptions: file checks" The lab is as follows: Objectives Familiarize the student with: situations when exceptions are thrown; handling file-related exceptions. Scenario Write a class that holds a (2×2) matrix, and add two methods to work with files: one method to load the matrix from a file (in any format) and one method to save the matrix to a file (in the same format). Add code to handle exceptional situations (file...

  • In order to do Program 6, Program 5 should bemodified.Program 6

    In order to do Program 6, Program 5 should be modified.Program 6-----------------------------------Program 4----------------------------------------------- (abstract class exception handling) Modify program4 to meet the following requirements: I. Make Person, Employee classes to abstract classes. Cl0%) 2. Add an interface interface working with a method teach() (5%) interface working void teach(String course) J 3. Modify Faculty class. (20%) a. a private data field and implement existing b. Modify Faculty class as a subclass of addition to the c. toString or print Info) method to...

  • Part 2: Programming with Exceptions In this part of the lab , you will write a...

    Part 2: Programming with Exceptions In this part of the lab , you will write a program that fetches the information stored at a give URL on the web and saves that data to a file. This will also include networking and file operations and partly an exercise in using exceptions. For doing I/O, Java has a pair of nice abstractions: InputStream and OutputStream. These are abstract classes in the package java.io. An InputStream is a place from which you...

  • Lab Exercise 05.1 Interest Rate PLEASE USE JAVA Compound interest is the way that you can...

    Lab Exercise 05.1 Interest Rate PLEASE USE JAVA Compound interest is the way that you can turn a little bit of money into a lot of money, if you have enough time. We have seen this calculation in a previous lab. This exercise will rewrite the program but will define a class named interestRate which will contain several separate methods. These methods will be incorporated into the class definition so that they can be tested by a main test program...

  • C programming. please include comments In this lab, you will learn to read data from and...

    C programming. please include comments In this lab, you will learn to read data from and write data to a file - another use for pointers. SAMPLE PROGRAM There are two sample programs this week - WriteFile.c and ReadFile.c. The first, WriteFile.c, allows the user to enter customer names (first, middle, and last), and writes each name to an output text file, customerNames.txt. formatted as: firstName middlelnitial lastName" After you've run it once, what happens to the names you added...

  • Objectives During this lab, students will learn how to 1. Implement a queue using arrays Instructions...

    Objectives During this lab, students will learn how to 1. Implement a queue using arrays Instructions For this lab you will be using your arrayQueueu algorithms that you completed for Homework 10. You are to implement a class called arrayQueue, along with a driver class arrayDriver and the customer exception class that will contain any exceptions that you may need to throw (i.e. emptyQueueException). This will be three seperate java files, and the package name will be PA5arrayQueue [please ensure...

  • Must be written in JAVA Code Write a program that will read in a file of student academic credit data and create a list of students on academic warning. The list of students on warning will be written...

    Must be written in JAVA Code Write a program that will read in a file of student academic credit data and create a list of students on academic warning. The list of students on warning will be written to a file. Each line of the input file will contain the student name (a single String with no spaces), the number of semester hours earned (an integer), the total quality points earned (a double). The following shows part of a typical...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT