Assignment 1
The goal of this assignment includes:
Practice of using basic Java features, including arrays, loops, and selection statements.
Practice of defining a Java class.
Practice of using Java ArrsayList
Part 1
Create a file named Hw1_part1.java and write the following methods in it:
Write a Java method named sumOfSquaresOfOdds that receives an integer n and returns
the sum of squares of all positive odd integers less than or equal to n.
Write a Java method named statistics that receives an array of double numbers, calculates the largest number, the smallest number, and the average of all numbers. The method, then, stores these three statistics, in that order, in an array of size 3 and returns the array.
Write a main method to test above two methods:
o InvokesumOfSquaresOfOddstwiceoncewithn=10andthenwithn=20,andprint each result on the screen.
o Create an array with 10 double numbers, and invoke statistics and pass this array as an argument, and print the result (max, min, average) on the screen. Repeat this with different set of 10 double numbers.
An incomplete code of Hw1_Part1.java is posted on Blackboard. You must complete this code.
Part 2
A class file named Person.java is posted on Blackboard. First, you must create a file named Employee.java that defines an Employee class as a subclass of the Person class. You must include the following instance variables and methods in the class definition:
Instance variables:
empId (int type) position (String type) salary (double type)
A constructor with the following arguments: name, age, empId, position, salary
Get methods: getEmpId()
getPosition()
getSalary()
Set methods:
setEmpId
setPosition
setSalary
An incomplete code of Employee.java, which includes a toString method, is posted on Blackboard. You must complete this code. I also posted Hw1_Part2.java, which you can use to test your class definition. In the main method, an employee object is created and the employee is printed. You are required to add a code segment that creates two more employee objects and prints them in the same manner,
Part 3
This part is a practice of using Javas ArrayList. Write a program named Hw1_Part3.java, which performs the following requirement.
Your program must read information about a certain number of employees from an input file named employee_info.txt. The input file has one employee in each line. A sample input is shown below:
Susan 32 2345 director 200000 Kelsey 45 3456 director 250000 Jake 27 4567 assistant 60000 Pederica 53 5678 CEO 500000 Yapsiong 36 6789 manager 100000
Each line has name, age, employee id, position, and salary.
Components in each line, typically referred to as tokens, are separated by a space.
Your program must create employee objects (of the employees in the input file) and store them in a Javas ArrayList.
Your program must print the following:
All employees in the ArrayList are:
Susan 32 2345 director 200000 Kelsey 45 3456 director 250000 Jake 27 4567 assistant 60000 Pederica 53 5678 CEO 500000 Yapsiong 36 6789 manager 100000
Employee with the highest salary is: Pederica 53 5678 CEO 500000
The youngest employee is:
Jake 27 4567 assistant 60000
Documentation
No separate documentation is needed. However, you must include sufficient inline comments within your program.
Deliverables
You must submit Hw1_part1.java, Employee.java, Hw1_part2.java, and Hw1_part3.java. Combine all four files (and other additional files, if any) into a single archive file, name it LastName_FirstName_hw1.EXT, where EXT is an appropriate file extension, such as zip of rar, and upload it to Blackboard.
Grading
Part 1: Each method will be tested with two different inputs and up to 5 points will be deducted for each wrong output (total possible deduction: 20 points).
Part 2: Your program will be tested with two employees and up to 10 points will be deducted for each wrong output (total possible deduction: 20 points).
Part 3: Your program will be tested with two input files and up to 15 points will be deducted for each wrong output (total possible deduction: 30 points).
Up to 20 points will be deducted if your program does not have sufficient inline comments.
Reviews
There are no reviews yet.