Jump to content

Java Programming Help (Class, Please help)

I have an assignment called time for school through the Edhesive AP Computer Science Course. I wrote out the entire program but now when I try and submit it for checking I don't get higher than a 50%. I don't know what is wrong because I have had multiple people look it over and nothing seems to be wrong.


Here's my code:



public class Time { private int hour; private int minute;
public Time() { hour = 12; minute = 0; this(12, 0); }
public Time(int h, int m) { hour = 0; minute = 0; if (h > 0 && h < 24) { hour = h; } if (m <= 59 && m >= 0) { minute = m; } }
public String toString() {
String hours = "";
String minutes = "";
if (hour < 10) {
hours += "0";
}
if (minute < 10) {
minutes = "0";
}
hours += hour;
minutes += minute;
return hours + minutes;
}
public String convert() {
String meridian;
if (hour >= 12) {
meridian = "pm";
} else {
meridian = "am";
}
String M1 = toString().substring(2,3);
String H1;
if ((hour - 12) > 0) {
H1 = "" + (hour - 12) + ":";
} else {
H1 = "" + (hour) + ":";
}
return (H1 + M1 + " " + meridian);
}



public void increment() {
minute ++;
if (minute == 60) {
hour += 1;
minute = 00;
}
if (hour == 24) {
hour = 00;
}

}
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


Here is the program that runs my code and test it:


/* * AP CS MOOC * Term 2 - Assignment 1: Time * Use this runner program to test your Assignment 1 solution. */



import java.io.IOException;
class student_time_runner {
public static void main(String str[]) throws IOException {
Time time1 = new Time(14, 56); System.out.println("time1: " + time1); System.out.println("convert time1 to standard time: " + time1.convert()); System.out.println("time1: " + time1); System.out.print("increment time1 five times: "); time1.increment(); time1.increment(); time1.increment(); time1.increment(); time1.increment(); System.out.println(time1 + "\n");
Time time2 = new Time(-7, 12); System.out.println("time2: " + time2); System.out.print("increment time2 67 times: "); for (int i = 0; i < 67; i++) time2.increment(); System.out.println(time2); System.out.println("convert to time2 standard time: " + time2.convert()); System.out.println("time2: " + time2 + "\n");
Time time3 = new Time(5, 17); System.out.println("time3: " + time3); System.out.print("convert time3: "); System.out.println(time3.convert());
Time time4 = new Time(12, 15); System.out.println("\ntime4: " + time4); System.out.println("convert time4: " + time4.convert());
Time time5 = new Time(0, 15); System.out.println("\ntime5: " + time5); System.out.println("convert time5: " + time5.convert());
Time time6 = new Time(24, 15); System.out.println("\ntime6: " + time6); System.out.println("convert time6: " + time6.convert());
Time time7 = new Time(23,59); System.out.println("\ntime7: " + time7); System.out.println("convert time7: " + time7.convert()); time7.increment(); System.out.println("increment time7: " + time7); System.out.println("convert time7: " + time7.convert());
}
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


Here's what is says is wrong with my code: Your code has been evaluated against a set of test data. 



You had 7 out of 14 tests pass correctly. Your score is 50%.
The tests that failed were: Test: Convert and Increment Incorrect: One Increment Incorrect: Multiple Increments Incorrect: More than 1440 Increments Test: Constructor and Convert Incorrect: Default Constructor Incorrect: Invalid Hour Incorrect: Invalid Minute Incorrect: Valid Time

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


Here is the Assignment explanation:


For this assignment, you will create a Time class that holds an hour value and a minute value to represent a time. We will be using "military time", so 12:01 am is 0001 and 1 pm is 1300. For this assignment, you may assume valid military times range from 0000 to 2359. Valid standard times range from 12:00 am to 11:59 pm. In previous assignments, we had a requirement that your class be named Main. In this assignment, the class is required to be named Time. To get started, download the template file, Time.java. Your job will be to add the constructors and methods described in the following sections to the class, Time, that is declared in this template file. Time should include two constructors: Time() - Default constructor that sets the time to 1200. Time(int h, int m) - If h is between 1 and 23 inclusive, set the hour to h. Otherwise, set the hour to 0. If m is between 0 and 59 inclusive, set the minutes to m. Otherwise, set the minutes to 0. Time should include the following methods: String toString() - Returns the time as a String of length 4 in the format: 0819. Notice that if the hour or minute is one digit, it should print a zero first. For example, 6 should print as 06. String convert() - Returns the time as a String converted from military time to standard time. For example, 0545 becomes 5:45 am and 1306 becomes 1:06 pm. void increment() - Advances the time by one minute. Remember that 60 minutes = 1 hour. Therefore, if your time was 0359, and you add one minute, it becomes 0400. 2359 should increment to 0000. To test your code, download the runner class Loading into the same folder that holds your Loading. Execute the method student_time_runner.main, and verify that the output matches the sample run listed below. We will use a similar but different runner to grade the program. In order to pass all tests, you will need to change student_time_runner.java to test different values to make sure your program fits the requirements. Note: You will not be submitting student_time_runner.java. This file is provided to help you test your implementation of the class Time. When you are done coding and testing, copy and paste your entire Time class into the Code Runner and press "Submit Answers" in order for your assignment to count as turned in. 



Sample Run:
time1: 1456 convert time1 to standard time: 2:56 pm time1: 1456 increment time1 five times: 1501
time2: 0012 increment time2 67 times: 0119 convert to time2 standard time: 1:19 am time2: 0119
time3: 0517 convert time3: 5:17 am
time4: 1215 convert time4: 12:15 pm
time5: 0015 convert time5: 12:15 am
time6: 0015 convert time6: 12:15 am
time7: 2359 convert time7: 11:59 pm increment time7: 0000 convert time7: 12:00 am

post-251526-0-48979600-1453500036_thumb.

Link to comment
Share on other sites

Link to post
Share on other sites

I think it's 15

CPU: Intel i7 5820K @ 4.20 GHz | MotherboardMSI X99S SLI PLUS | RAM: Corsair LPX 16GB DDR4 @ 2666MHz | GPU: Sapphire R9 Fury (x2 CrossFire)
Storage: Samsung 950Pro 512GB // OCZ Vector150 240GB // Seagate 1TB | PSU: Seasonic 1050 Snow Silent | Case: NZXT H440 | Cooling: Nepton 240M
FireStrike // Extreme // Ultra // 8K // 16K

 

Link to comment
Share on other sites

Link to post
Share on other sites

Must you

have you tried turning it off and on again?

 

 

that thing is horrible to read, I'm sorry

could you please use the [code ] tags for it?

CPU: Intel i7 5820K @ 4.20 GHz | MotherboardMSI X99S SLI PLUS | RAM: Corsair LPX 16GB DDR4 @ 2666MHz | GPU: Sapphire R9 Fury (x2 CrossFire)
Storage: Samsung 950Pro 512GB // OCZ Vector150 240GB // Seagate 1TB | PSU: Seasonic 1050 Snow Silent | Case: NZXT H440 | Cooling: Nepton 240M
FireStrike // Extreme // Ultra // 8K // 16K

 

Link to comment
Share on other sites

Link to post
Share on other sites

I don't see any return statement from your time1.increment() void?

umm.. it's a void obviously don't have to return

CPU: Intel i7 5820K @ 4.20 GHz | MotherboardMSI X99S SLI PLUS | RAM: Corsair LPX 16GB DDR4 @ 2666MHz | GPU: Sapphire R9 Fury (x2 CrossFire)
Storage: Samsung 950Pro 512GB // OCZ Vector150 240GB // Seagate 1TB | PSU: Seasonic 1050 Snow Silent | Case: NZXT H440 | Cooling: Nepton 240M
FireStrike // Extreme // Ultra // 8K // 16K

 

Link to comment
Share on other sites

Link to post
Share on other sites

umm.. it's a void obviously don't have to return

 

 

I will now go and kill myself.. realised what I'd said and came back to remove it.. beat me to it.

 

*bleh*

Link to comment
Share on other sites

Link to post
Share on other sites

Must you

are your program outputs the same as provided in the test sample?

CPU: Intel i7 5820K @ 4.20 GHz | MotherboardMSI X99S SLI PLUS | RAM: Corsair LPX 16GB DDR4 @ 2666MHz | GPU: Sapphire R9 Fury (x2 CrossFire)
Storage: Samsung 950Pro 512GB // OCZ Vector150 240GB // Seagate 1TB | PSU: Seasonic 1050 Snow Silent | Case: NZXT H440 | Cooling: Nepton 240M
FireStrike // Extreme // Ultra // 8K // 16K

 

Link to comment
Share on other sites

Link to post
Share on other sites

are your program outputs the same as provided in the test sample?

I reformatted the post to make it a little easier to read.

Link to comment
Share on other sites

Link to post
Share on other sites

This is what your code gives me in the output...

run:time1: 1456convert time1 to standard time: 2:5 pmtime1: 1456increment time1 five times: 1501time2: 0012increment time2 67 times: 0119convert to time2 standard time: 1:1 amtime2: 0119time3: 0517convert time3: 5:1 amtime4: 1215convert time4: 12:1 pmtime5: 0015convert time5: 0:1 amtime6: 0015convert time6: 0:1 amtime7: 2359convert time7: 11:5 pmincrement time7: 0000convert time7: 0:0 amBUILD SUCCESSFUL (total time: 0 seconds)
Can see some strange behaviour already...
Link to comment
Share on other sites

Link to post
Share on other sites

Moved to Programming and Software Design. 

15" MBP TB

AMD 5800X | Gigabyte Aorus Master | EVGA 2060 KO Ultra | Define 7 || Blade Server: Intel 3570k | GD65 | Corsair C70 | 13TB

Link to comment
Share on other sites

Link to post
Share on other sites

String M1 = toString().substring(2,3); should be String M1 = toString().substring(2,4); This will fix the problem with the minutes printing incorrectly, since the substring length was originally incorrect. 

 

Now, you need to correct your convert() method. It seems to be printing out a 0 when it should be printing a 12 for the hours in some cases. check time 5, 6, and the increment of time 7. I have found a way to make it work but you should be able to figure that out on your own.

Link to comment
Share on other sites

Link to post
Share on other sites

Just as a note, not commenting any of your code can cause lecturers to take away marks.

 

Always comment your code.

Case: Corsair Graphite Series 600T CPU: Intel Core i7 - 4770k @ 3.50GHz Motherboard: ASUS Z87-K GPU: GTX 1060 Ram: Corsair Vengeance 8GB x2

 

Storage : Samsung 840 Evo 500gb SSD, Misc HDD's Monitors: BenQ GL2450 x2 Mouse: Razer Naga Epic OS: Win10

 

 

Laptop: ASUS - X555LA-DM1672T CPU: Intel Core i5 5200U Ram: 8GB Storage: Samsung 840 Evo 500gb SSD 1920x1080 OS: Windows 10/Ubuntu

 

Link to comment
Share on other sites

Link to post
Share on other sites

Just as a note, not commenting any of your code can cause lecturers to take away marks.

 

Always comment your code.

what u smoke?

automated online tests don't check for comments

CPU: Intel i7 5820K @ 4.20 GHz | MotherboardMSI X99S SLI PLUS | RAM: Corsair LPX 16GB DDR4 @ 2666MHz | GPU: Sapphire R9 Fury (x2 CrossFire)
Storage: Samsung 950Pro 512GB // OCZ Vector150 240GB // Seagate 1TB | PSU: Seasonic 1050 Snow Silent | Case: NZXT H440 | Cooling: Nepton 240M
FireStrike // Extreme // Ultra // 8K // 16K

 

Link to comment
Share on other sites

Link to post
Share on other sites

what u smoke?

automated online tests don't check for comments

Most of the time the automated online tests are only done to check if the code works.

After that, at least in my experience, the code will be checked by the lecturers or assistants. Mostly so that they can give feedback and critique your coding.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×