Write a program to read a number of grades from the user. The user will first enter the number of grades that will be entered, then the grades. At the end, you should print out the average of the grades.
Notes:
You must give the user many more attempts when they have entered a value that is not an integer.
You must give the user many more attempts if they enter an integer value that is not acceptable (outside of the range 0 - 100).
When calculating the average, you should use a double (if you use float the answer might be slightly different).
Hint for allowing the user to make multiple attempts:
To allow the user many attempts to enter the correct type and a value in the correct range, you will need to use two nested loops, the outer loop should check if the text if the value is in the correct range and an inner loop that removes any tokens entered that are not the correct type.
Write a program to read some student information from the command prompt. This information should be used to create Student objects (based on this Student class).
Your program should contain the following operations: (you do not need to define the Student class, but do not forget to import the scanner)
Define and construct a scanner to read input from the user
Define and construct an array of Student objects of size 3
Read the first name of a student from the user and store it in a variable
Read the family name of a student from the user and store it in a variable
Read the number of a student from the user and store it in a variable
Read the age of a student from the user and store it in a variable
Read the name of the degree that the student is studying and store it in a variable
Construct a student object based on the values read from the user and store it in the array
Repeat the process two more times
In reverse order, print the student number and degree of each student on a single line
publicclassprog{ publicstaticvoidmain(String[] args) { Scannerinput=newScanner(System.in); Student[] students = newStudent[3]; for (inti=0; i < students.length; i++) { Stringn= input.next(); Stringfn= input.next(); intsn= input.nextInt(); inta= input.nextInt(); Stringdeg= input.nextLine(); students[i] = newStudent(n, fn, sn, a, deg); }
for (inti= students.length-1; i >= 0; i--) { System.out.println(students[i].getStudentNumber() + " " + students[i].getDegreeName()); } } }
Programming 05 Encapsulation
Define a class named Time. This class should represent an instant in time during a day (as a number of hours, minutes and seconds) and provide some utility methods too.
The class should have the following functionality:
Constructors
A constructor that takes three int parameters in the order hours, minutes, seconds
Constants
A constant named SECONDS_PER_MINUTE, that has a value of 60
A constant named MINUTES_PER_HOUR, that has a value of 60
A constant named HOURS_PER_DAY, that has a value of 24
Instance Methods
A method named getSeconds, that takes no parameters and returns an int representing the number of seconds since the start of the last minute (0 - 59)
A method named getMinutes, that takes no parameters and returns an int representing the number of minutes since the start of the last hour (0 - 59)
A method named getHours, that takes no parameters and returns an int representing the number of hours since the start of the last day (0 - 23)
A method named timeBetween, that takes another Time object as a parameter and returns an int representing the number of seconds between these two times. The value should always be positive.
A method named sameTime, that takes another Time object as a parameter and returns a boolean value. The return value should be true if both time objects represent the same time and false if they do not.
A method named asString, that takes no parameters and returns a string containing the current time in the format hh:mm:ss
A method named setTime, that takes three int parameters in the order hours, minutes, seconds and returns nothing. This method should set the time to the values provided only if it represents a valid time.
Class Methods
A class method named isValidTime, that takes three int parameters in the order hours, minutes, seconds and returns a boolean value. The return value should be true if the values represent a valid time and false if they do not.
Notes
You should base this work on the earlier question in programming problems quiz 02. However, it is expected that you apply the knowledge learned from the lecture about encapsulation to the class.
publicclassTime { publicstaticfinalintMINUTES_PER_HOUR=60; publicstaticfinalintSECONDS_PER_MINUTE=60; publicstaticfinalintHOURS_PER_DAY=24; privateint seconds; publicTime(int h, int m, int s) { seconds = s + m * SECONDS_PER_MINUTE + h * MINUTES_PER_HOUR * SECONDS_PER_MINUTE; }
publicbooleansameTime(Time t) { return t.seconds == seconds; } public String asString() { return String.format("%02d:%02d:%02d", getHours(), getMinutes(), getSeconds()); } publicvoidsetTime(int h, int m, int s) { if (isValidTime(h, m, s)) { seconds = s + m * SECONDS_PER_MINUTE + h * MINUTES_PER_HOUR * SECONDS_PER_MINUTE; } }
publicstaticbooleanisValidTime(int h, int m, int s) { if (h < 0 || h >= HOURS_PER_DAY || m < 0 || m >= MINUTES_PER_HOUR || s < 0 || s >= SECONDS_PER_MINUTE) { returnfalse; } returntrue; }
}
Programming 06 Interfaces
Your teacher has too many tasks to complete, and needs help with tracking them. Unfortunately, they tasks are all of different types but have some commonalities that can be extracted to the following interface:
The first method returns a string containing a description of the task to be completed. The second method returns a rating indication how much the person desires to complete the task as a number between 1 and 5 (inclusive). The final method returns a Duration object detailing the amount of time the task will take to complete.
Write a class called TaskManager that can be used to track the details of some tasks. The class should have the following methods:
A class named SalariedEmployee which inherits from the Employee class
A class named HourlyPaidEmployee which inherits from the Employee class
A class named Contractor which inherits from the Employee class
A public class named Main, which contains a main method
Numbers
All numbers in this example are calculated in cents. There are 100 cent in 1 euro. This is because it is always a bad idea to use floating point numbers for money. As such all values should be calculated and returned as integers.
Employee Class
The Employee class is abstract and contains an abstract method for calculating the weekly payroll cost of an employee. This must be implemented in the subclasses that you define. The Employee class also contains a getTax method, which calculates tax as 10% of the weekly payroll cost of an employee.
SalariedEmployee
This class is very similar to an Employee, except it also should be able to remember the salary of the employee and use this to calculate the weekly payroll cost. Note that salaries are stated by the amount of money the person will earn in one year, you will need to calculate how much that will cost per week.
HourlyPaidEmployee
This class is very similar to an Employee, except it also should be able to remember the hourly wage of an employee and the number of hours they work every week. These values should be used to calculate the weekly payroll cost for these employees.
Contractor
A contractor is a special type of employee who is paid to complete a task, but the company does not have to pay any tax for them.
Main Class
Write a program that completes the following steps
Reads each line of user input and creates the appropriate type of employee object
Add all of the employee objects to an array (there will be a maximum of 100)
Use the calculateTotalWeeklyPayroll and calculateTotalWeeklyTax methods in the PayrollSystem class to calculate and output those values.
Input Text
The input from the user will contain the information about a single employee on each line. The line will start with the word “Salary” it the employee is paid a salary, “Hourly” if the employee is paid an hourly rate, and “Contractor” if the employee is a contractor.
Lines containing information about a salaried employee will contain the following values separated by spaces:
The employees name (just one word)
The employees id number (a string starting with the letter P)
The employees salary as an integer in cents
Lines containing information about a hourly paid employees will contain the following values separated by spaces:
The employees name (just one word)
The employees id number (a string starting with the letter P)
The employees hourly rate of pay as an integer in cents
The number of hours that this employee works every week
Lines containing information about a contractor will contain the following values separated by spaces:
The contractors name (just one word)
The contractors id number (a string starting with the letter P)
The contractors amount that is paid to the contractor every week in cents.
Given the Time class. Define a class named Instant that inherits from the Time class. The instant class should add the functionality to remember an amount of milliseconds along with a value of hours, minutes and seconds.
To this class you should add the following methods:
a toString method which takes no parameters and returns a string containing the representation from the time object followed by milliseconds (this should use the inherited toString method from the Time class)
a sameInstant method which takes an Instant object as a parameter and returns boolean if both objects represent the same time (this should use the inherited sameTime method from the Time class)
a milliSecondsBetween method takes an Instant object as a parameter and returns and int value representing the difference between the two instants counted in miliseconds (this should use the inherited timeBetween method from the Time class)
In addition, you should define a public class named Main. In this class you should define the following static methods:
A method named readAndTestToString that reads four space separated integers from the user and uses them to create an Instant object. This object should then be printed to the screen.
The numbers will be in the order hours minutes seconds milliseconds
A method named readAndTestSameInstant that reads two lines of space separated integers from the user and uses them to create instant objects. A message should be printed to the screen if the two objects represent the same instant.
A method named readAndTestMilliSecondsBetween that reads two lines of space separated integers from the user and uses them to create instant objects. A message should be printed to the screen showing the number of milliseconds between the two instant objects.
The Time class has the following public interface.
1 2 3 4 5 6 7
publicTime(int h, int m, int s) publicintgetSeconds() publicintgetMinutes() publicintgetHours() publicinttimeBetween(Time t) publicbooleansameTime(Time t) public String toString()
publicstaticvoidreadAndTestMilliSecondsBetween(){ Scanners=newScanner(System.in); Instanta=newInstant(s.nextInt(), s.nextInt(), s.nextInt(), s.nextInt()); Instantb=newInstant(s.nextInt(), s.nextInt(), s.nextInt(), s.nextInt()); System.out.println(a + " and " + b + " are "+a.milliSecondsBetween(b)+" millisecond apart"); }
publicstaticvoidreadAndTestSameInstant(){ Scanners=newScanner(System.in); Instanta=newInstant(s.nextInt(), s.nextInt(), s.nextInt(), s.nextInt()); Instantb=newInstant(s.nextInt(), s.nextInt(), s.nextInt(), s.nextInt()); if (a.sameInstant(b)){ System.out.println(a + " and " + b + " are the same instant"); } else { System.out.println(a + " and " + b + " are not the same instant"); } }
Given the Time class, modify it such that it can be sorted using the sort methods from Arrays or Collections. This should be done by implementing the Comparable interface with the correct type parameter.
classTimeimplementsComparable<Time> { privateint seconds; privateint minutes; privateint hours; publicTime(int h, int m, int s) { if (seconds < 0 || seconds > 59) { thrownewIllegalArgumentException("Seconds must be between 0 and 59"); } if (minutes < 0 || minutes > 59) { thrownewIllegalArgumentException("Minutes must be between 0 and 59"); } if (hours < 0 || hours > 23) { thrownewIllegalArgumentException("Hours must be between 0 and 23"); } seconds = s; minutes = m; hours = h; }
publicintgetSeconds() { return seconds; }
publicintgetMinutes() { return minutes; }
publicintgetHours() { return hours; }
public String toString() { return String.format("%02d:%02d:%02d", getHours(), getMinutes(), getSeconds()); }
Given the Time class which you may not modify, create a comparator (named TimeComparator) which implements the Comparator interface with the correct type parameter. The result should be that time objects can be sorted using the sort methods from Arrays or Collections. The time values should be sorted into descending order.