SRL Archive - News and General

Help me with java!


TViYH

2009-04-02 07:47:49

ComS 227
Homework Assignment 3 300 points Due date: Friday, Apr 3, 2009, 11:59pm

Introduction
In homework 2 we have developed a Duration class used to represent a period of time. This homework will use this Duration class together with a Time class to keep track of weekly exercises. This assignment uses runtime exceptions to guard against illegal values such as a negative time. You will also use an enumerated type in this homework to represent the day of the week.

Detailed Class Specification
Enum Day
A file called Day.java is given. The file contains the following definition:
public enum Day {
Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday}

You should not need to change this file. Note that a useful method that is available to all enumerated types is called ordinal(). It returns the ordinal number of an enumerated type constant. For example, Day.Sunday.ordinal() returns 0, and Day.Tuesday.ordinal() returns 2.

Duration Class
The Duration class is the same as in Assignment 2. In this homework assignment, your Duration class itself will not be graded again, but you are responsible for its correctness wherever else it is used in this assignment.

Time Class
The Time class represents a time in hours, minutes, and seconds. It has the following requirements. All the constructor and methods specified below are public. You may have other methods for this class, but they must be set to be private.

1. No default constructor.

2. A specific constructor that takes hours, minutes, and seconds as parameters and creates a time object with the specific hours, minutes, and seconds. The constructor throws a RuntimeException if the seconds is not between 0 and 59, the minutes is not between 0 and 59, or the hours is not between 0 and 23. Note that this is 24-hour time and 22 hours, 31 minutes, 15 seconds represents 10:31:15 pm.

3. The methods getHours, getMinutes, and getSeconds are required and return int-type hours, minutes, and seconds, respectively.

4. A toString method that returns the time in 12-hour format is required. For example, 21 hours, 39 minutes, 22 seconds returns the string “9:39:22 PM”.

5. A static method called subtract that takes two Time objects as parameters and returns a Duration object that is the difference between the two Time objects. Note that if the first Time parameter is less than the second Time parameter, a RuntimeException is thrown.

GymWeeklyExerciseManager Class
This class represents a weekly exercise regimen for the members of a gym. It has the following requirements. All the constructor and methods specified below are public. You may have other methods for this class, but they must be set to be private.
Note that in this assignment, a person is considered to be in the gym starting from the second he/she checks in. A person is considered to be out of the gym starting from the second he/she checks out.

1. A default constructor.

2. A method called checkIn with the following signature: void checkIn(String name, Day day, Time checkInTime);

The method registers the time of day a given person checks in. If the person has already checked in, the method throws a RuntimeException. In other words, a checkIn must be closed by a checkout before a new checkIn for the same person is invoked; otherwise, a RuntimeException is thrown.

3. A method called checkOut with the following signature: void checkOut(String name, Time checkOutTime); This method registers the time a person checks out. Note that there is no day parameter, since the checkout has to happen on the same day as the check-in. If the checkout time is
not later than the time the person checked in or the person is not checked-in, this method throws a RuntimeException.

4. A method called getTotalGymTime which contains an array that sums the amount of time spent in the gym by all users during each day of the week (Sunday through Saturday). This method throws a RuntimeException if there is any user left unchecked out. Duration[] getTotalGymTime();

5. A method called getExerciseTimeOf. It returns an array of Duration which contains the total amount of time that a person called “name” spent in the gym during each day of the week, from Sunday and Saturday. If the person did not check into the gym on any given day, then the Duration value for that day is 0. This method throws a RuntimeException if the person called “name” is left unchecked out when the method is being invoked. Duration[] getExerciseTimeOf(String name);

6. A method called getBusiestDay which returns the day when the total amount of time spent in the gym by all users is highest. This method throws a
RuntimeException if there is any user left unchecked out. Day getBusiestDay();

7. A method called whoIsInGym which returns an array of the names of everyone who is in the gym at the specified time. This method throws a RuntimeException if there is any user left unchecked out. String[] whoIsInGym(Day day, Time time);

8. A method called getMaximumOccupancyOfTheWeek which returns the hightest number of people checked into the gym at the same time. This method throws a RuntimeException if there is any user left unchecked out. int getMaximumOccupancyOfTheWeek();

9. A method called isInGym which returns true if the person called “name” is in the gym between the start and end time, inclusive. This method throws a RuntimeException if there is any user left unchecked out, or if the start time occurs later than the end time. boolean isInGym(String name, Day day, Time start, Time end);

10. A method called readCSVFile which reads information from a comma-separated values ( CSV ) file. The method has the following signature: void readCSVFile(String filename);

The CSV file has the following format:
Name,Day,checkInTime,checkOutTime. For example, a CSV file may look like:
Sara Smith,Monday,17:30:00,18:00:00
Joe Smith,Wednesday,00:08:00,00:48:01
Sara Smith,Monday,07:30:00,08:10:00

Note that a person may come to the gym several times on a given day. If the CSV file is in a format other than specified, this method throws a RuntimeException.
You are responsible for thoroughly testing each of the above constructors and methods. Feel free to share any test programs that may use these methods or any CSV files.

I have the Duration class done:

//HOMEWORK 3- The Duration class that is used by the Exercise Calculator

public class Duration {
private int hours;
private int minutes;
private int seconds;

//Default constructor that sets the variables to zero
public Duration(){
hours = 0;
minutes = 0;
seconds = 0;
}

//This constructor takes the hours, minutes, and seconds as parameters and sets them
//into the proper format with no more than 59 in the seconds and minutes
public Duration(int hr, int min, int sec){
if (min > 59){
hr = hr + min / 60;
min = min % 60;
}

if (sec > 59){
min = min + sec / 60;
sec = sec % 60;
}

hours = hr;
minutes = min;
seconds = sec;
}

//This constructor takes just seconds as the parameter. It then takes the seconds and
//converts it to the hours, minutes, and seconds format.
public Duration(int totalSeconds){
hours = totalSeconds / 3600;
totalSeconds = totalSeconds % 3600;
minutes = totalSeconds / 60;
seconds = totalSeconds % 60;
}

//This takes a Duration object as a parameter and then copies the info inside it to a
//different Duration object.
public Duration(Duration other){
hours = other.getHours();
minutes = other.getMinutes();
seconds = other.getSeconds();
}

//This returns the hours for the object called on.
public int getHours(){
return hours;
}

//This returns the minutes for the object called on.
public int getMinutes(){
return minutes;
}

//This returns the seconds for the object called on.
public int getSeconds(){
return seconds;
}

//This method sets the hours
public void setHours(int hr){
hr = hours;
}

//This method takes any int and separates into minutes and hours
public void setMinutes(int min){
if(min > 59){
hours = hours + min / 60;
minutes = min % 60;
}
else min = minutes;
}

//This method takes any int as seconds and separates it into minutes and seconds
public void setSeconds(int sec){
if(sec > 59){
minutes = minutes + sec / 60;
seconds = sec % 60;
}
else sec = seconds;
}

//This method takes a duration object as a parameter and adds the hours, minutes, and seconds
//of that object to the object that is already running.
public Duration add(Duration plus){
Duration n = new Duration(hours+plus.getHours(), minutes+plus.getMinutes(), seconds+plus.getSeconds());
return n;
}

//This method takes an int as an argument and multiplies the hours, minutes, and seconds each
//by the the int passed in.
public Duration multiply(int n){
Duration s = new Duration(hours * n, minutes* n, seconds * n);
return s;
}

//This returns the hours, minutes, and seconds of an object in a readable string
public String toString(){
return (hours+" hours "+minutes+" minutes "+seconds+" seconds");
}

//This takes a Duration object as an argument and tests if the hours, minutes, and seconds are the
//same as the object already running. It returns true if they are equal and false if they are not
//equal or if the object equals null.
public boolean equals(Duration equal){
if (equal == null){
return false;
}
if(hours == equal.getHours() && minutes == equal.getMinutes() && seconds == equal.getSeconds()){
return true;
}
return false;
}

//This takes a Duration object and compares it to the object already running and returns 1 if the object
//already running is bigger, -1 if it is smaller, and 0 if they are the same.
public int compareTo(Duration compare){
int sec = compare.getHours()*3600 + compare.getMinutes()*60 + compare.getSeconds();
int originalSec = hours*3600 + minutes*60 + seconds;
if(sec > originalSec) return -1;
if(sec < originalSec) return 1;
return 0;
}
}

I need to get the compareTo method done in the time class and then its done:

public class Time {

private int hours;
private int minutes;
private int seconds;

public Time(int hours1, int min1, int sec1) throws Exception {

if (sec1 < 0 || sec1 > 59 || min1 < 0 || min1 > 59 || hours1 < 0
|| hours1 > 23) {
throw new Exception("Invalid time entry");
}

hours = hours1;
minutes = min1;
seconds = sec1;
}

// This returns the hours for the object called on.
public int getHours() {
if (hours > 12) {
hours -= 12;
}
return hours;
}

// This returns the minutes for the object called on.
public int getMinutes() {
return minutes;
}

// This returns the seconds for the object called on.
public int getSeconds() {
return seconds;
}

// This returns the hours, minutes, and seconds of an object in a readable
// string
public String toString() {
String output;

if (hours > 12)
output = hours % 12 + ":" + minutes + ":" + seconds + " PM";
else if(hours == 12)
output = "12:" + minutes + ":" + seconds + " PM";
else
output = hours % 12 + ":" + minutes + ":" + seconds + " AM";
return output;
}

public static Duration subtract(Time t1, Time t2) throws Exception {
{
int hr_dif, min_dif, sec_dif;

if (t1.getHours() < t2.getHours()) {
throw new Exception("Can't Subtract times");
} else {
hr_dif = t1.getHours() - t2.getHours();
min_dif = t1.getMinutes() - t2.getMinutes();
sec_dif = t1.getSeconds() - t2.getSeconds();
}

if(min_dif<0){
min_dif += 60;
hr_dif -= 1;
}
if(sec_dif<0){
sec_dif += 60;
min_dif -= 1;
}

// create new Duration object with the difference in time
Duration time_dif = new Duration(hr_dif, min_dif, sec_dif);

return time_dif;
}
}

public int compareTo(Time compare){

}

}

I need to get this next one done and then the assignment is done, I have it outlined somewhat but i;m not sure if it is right. I used a subclass in it:

public class GymWeeklyExerciseManager {

private String guestname;
private String weekday;

public GymWeeklyExerciseManager() {
guestname = null;
weekday = null;
}

public void getGetTotalGymTime() {

}

public Day getBusiestDay() {

}

public String whoIsInGym() {

}

public int getMaximumOccuapancyOfWeek() {

}

public void readCSVFile(String filename) {

}

private class UserCred {

private String name;
private Time checkIn;

private void checkIn(String name, Day day, Time checkInTime) {
checkIn = checkInTime;

}

public void checkOut(String name, Time checkOutTime) {

}

public Duration getExerciseTimeOf() {

}

public boolean isInGym(String name, Day day, Time start, Time end) {

}

}

}

HELP ME!

TViYH

2009-04-02 20:29:33

^.^ Come on guys!

I might give a small $$ payment for help :)

Method

2009-04-02 23:05:28

Time should implement Comparable if you want to use the compareTo method. The parameter should be of type Object, also, which you will then type cast to Time. After that, it's as simple as doing something like:

public int compareTo(Object obj) {
Time other = (Time) obj; //if you want to make sure it can be type casted, use an if statement with the instanceof operator
return (hours + minutes + seconds) - (other.hours + other.minutes + other.seconds);
}

I'll edit back with the other stuff if I get to it.

EDIT: I'm probably not going to get to it, sorry. Consider using a Person class to keep track of all of the information you read in.