The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Java Help
|
Help with my program
Discuss Help with my program in the Java Help forum on Dev Shed. Help with my program Java Help forum discussing all Java platforms - J2ME, J2SE and J2EE - as well as relevant standards, APIs and frameworks such as Swing, Servlets, JSPs, Applets, Struts, Spring, Hibernate, ANT, EJB, and other Java-related topics.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

March 10th, 2013, 09:31 PM
|
|
Contributing User
|
|
Join Date: Oct 2012
Posts: 31
Time spent in forums: 7 h 8 m 13 sec
Reputation Power: 1
|
|
|
Help with my program
Hello! I just need some help/clarification on what to do for my project.
So my project utilizes 2 classes. 1 called System Tasks which asks for user input for a month, day, time, message and displays it into a list and another called Work Orders where I create an array of 20 objects based on the System Tasks class and choose between adding, deleting, or listing my System Tasks. Both classes use my first project called User Input which asks for character, String, integer, and double input.
The required methods I need to implement in this class are:
Code:
WorkOrders () constructor that places the 4 default SystemTask objects in the array
main() method the creates the WorkOrders object, then calls a run method
run() method that displays the menu, gets input, acts on that input
compareSystemTask (SystemTask A1, SystemTask A2) method that returns true if A1 < A2, false otherwise
insertSystemTask (SystemTask A1) places A1 in the proper (sorted) slot of the array
listSystemTask () method lists all SystemTask objects in the array (in order) with a number in front
deleteSystemTask () delete an object from the array using the number listSystemTask () outputs in front of the item
addSystemTask () calls inputSystemTask () from the SystemTask class and places it in the proper position of the array. Use an algorithm that shifts objects in the array (if needed) to make room for the new object. DO NOT sort the entire array, just shift objects
I also have to preload my array with the following objects:
Code:
Mar 4, 21:30 Backup Users
Apr 1, 17:00 Upgrade Hard Drives
May 6, 10:45 Virus Scan
Jun 3, 09:15 Database Backup
and here's what I have so far for my Work Orders class:
Code:
package Project1;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author r0nu5
*/
import Project0.UserInput;
import java.util.Scanner;
public class WorkOrders {
static SystemTask object = new SystemTask();
private SystemTask []arrayobject = new SystemTask[20];
private int nElems;
static char Menu_Option;
WorkOrders(){
arrayobject[0] = new SystemTask("Mar",4,21,30,"Backup Users");
arrayobject[1] = new SystemTask("Apr",1,17,00,"Upgrade Hard Drives");
arrayobject[2] = new SystemTask("May",6,10,45,"Virus Scan");
arrayobject[3] = new SystemTask("Jun",3,9,15,"Database Backup");
}
public static void main(String[] args) throws Exception {
WorkOrders object = new WorkOrders();
object.run();
}
public void run() throws Exception {
while (Character.toUpperCase(Menu_Option) != 'E') {
System.out.println("Select an Option");
System.out.println("A)dd SystemTask");
System.out.println("D)elete SystemTask");
System.out.println("L)ist SystemTask");
System.out.println("E)xit");
Menu_Option = (UserInput.getChar());
switch (Character.toUpperCase(Menu_Option)) {
case 'A':
addSystemTask();
break;
case 'D':
deleteSystemTask();
break;
case 'L':
System.out.println(listSystemTask());
break;
case 'E':
break;
}
}
}
public boolean compareSystemTask(SystemTask A1, SystemTask A2) {
return false;
}
public void insertSystemTask(SystemTask A1){
}
public String listSystemTask(){
String list = ("All the values in the array are: " + "\n" +
arrayobject[0] + arrayobject[1] + arrayobject[2] + arrayobject[3] +
arrayobject[4]);
return list;
}
public void deleteSystemTask(){
}
public void addSystemTask(){
object.inputSystemTask();
}
}
The problem I have is that I'm having some difficulties with understanding how to actually code some of the methods. I have a pretty good idea of what it's suppose to do, I'm just not entirely sure how to actually do it.
I originally tried putting this for my listSystemTask method just to see if it would display what I preloaded into the array:
Code:
System.out.println(arrayobject[0]);
System.out.println(arrayobject[1]);
System.out.println(arrayobject[2]);
System.out.println(arrayobject[3]);
but the result I got:
Code:
Time: 00:00
Message: null
Date: null 0
Time: 00:00
Message: null
Date: null 0
Time: 00:00
Message: null
Date: null 0
Time: 00:00
Message: null
Please help me! It would be greatly appreciated
I can also put up the code for my System Tasks & User Input classes if needed.
|

March 11th, 2013, 11:23 AM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
|
Where is the code that saves the values in the class objects?
Where is the code that formats the values into a String?
Have you checked that the data is correctly saved so it can later be formatted for display?
|

March 11th, 2013, 03:47 PM
|
|
Contributing User
|
|
Join Date: Oct 2012
Posts: 31
Time spent in forums: 7 h 8 m 13 sec
Reputation Power: 1
|
|
Quote: | Originally Posted by NormR Where is the code that saves the values in the class objects?
Where is the code that formats the values into a String?
Have you checked that the data is correctly saved so it can later be formatted for display? |
Actually, I think I don't have code for that...
How would I do that?
Oh and here is my System Tasks class. Could help me in creating the code you asked for?
Code:
package Project1;
import Project0.*;
public class SystemTask {
private int day;
private int hour;
private int minute;
private String month;
private String message;
public SystemTask() {
month = null;
message = null;
day = 1;
hour = 0;
minute = 0;
}
public SystemTask(String newMonth, int newDay, int newHour, int newMinute,
String newMessage) {
setDay(day);
setHour(hour);
setMinute(minute);
setMonth(month);
setMessage(message);
}
public static void main(String[] args) {
SystemTask object = new SystemTask();
object.inputSystemTask();
System.out.print(object.toString());
}
public void inputSystemTask() {
System.out.print("Input day: ");
day = UserInput.getInt(0, 31);
setDay(day);
System.out.print("Enter the hour: ");
hour = UserInput.getInt(0, 12);
setHour(hour);
System.out.print("Enter the minute: ");
minute = UserInput.getInt(0, 59);
setMinute(minute);
System.out.print("Enter the Month: ");
month = UserInput.getString(3, 3);
setMonth(month);
System.out.print("Enter your message: ");
message = UserInput.getString();
setMessage(message);
}
public void setDay(int Day) {
day = Day;
}
public void setHour(int Hour) {
hour = Hour;
}
public void setMinute(int Minute) {
minute = Minute;
}
public void setMonth(String Month) {
month = Month;
}
public void setMessage(String Message) {
}
public int getDay() {
return day;
}
public int getHour() {
return hour;
}
public int getMinute() {
return minute;
}
public String getMonth() {
return month;
}
public String getMessage() {
return message;
}
public String toString() {
String Clock = String.format("%02d:%02d", hour, minute);
String output = "Date: " + getMonth() + " " + getDay() + "\n"
+ "Time: " + Clock + "\n"
+ "Message: " + getMessage() + "\n";
return output;
}
}
|

March 11th, 2013, 03:59 PM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
You need to do some debugging to see what the code is doing incorrectly.
These lines in the WorkOrders constructor show create SystemTask objects with data for all the fields.
Code:
arrayobject[0] = new SystemTask("Mar",4,21,30,"Backup Users");
Where does the code create SystemTask objects without any data? That would happen if this constructor is called:
Code:
public SystemTask() {
Is that constructor called? Add a println statement in that constructor that prints a message if it is called.
The SystemTask class's toString() method builds the String that is printed by this statement:
Code:
System.out.println(arrayobject[0]);
You need to find out how the empty SystemTask objects got in the arrayobject array.
|

March 11th, 2013, 04:14 PM
|
|
Contributing User
|
|
Join Date: Oct 2012
Posts: 31
Time spent in forums: 7 h 8 m 13 sec
Reputation Power: 1
|
|
Quote: | Originally Posted by NormR
Where does the code create SystemTask objects without any data? That would happen if this constructor is called:
Code:
public SystemTask() {
Is that constructor called? Add a println statement in that constructor that prints a message if it is called.
|
Ok so I tried putting a println statement into that constructor. It displays the statement, so I know it's being called.
What are some ways to see what's causing the empty SystemTask objects to get into my array?
|

March 11th, 2013, 04:17 PM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
At the end of the constructor print out the value of this to see what was saved:
Code:
System.out.println("ST constr " + this);
The "this" will cause the toString() method to be called which will return all the data values for the class object.
Also add a println statement to some of the set.. methods to print out the values that were passed to the method.
Last edited by NormR : March 11th, 2013 at 04:21 PM.
|

March 11th, 2013, 04:25 PM
|
|
Contributing User
|
|
Join Date: Oct 2012
Posts: 31
Time spent in forums: 7 h 8 m 13 sec
Reputation Power: 1
|
|
Quote: | Originally Posted by NormR At the end of the constructor print out the value of this to see what was saved:
Code:
System.out.println("ST constr " + this);
The "this" will cause the toString() method to be called which will return all the data values for the class object.
Also add a println statement to some of the set.. methods to print out the values that were passed to the method. |
Alright, I tried putting it into the constructor and the result I got:
Code:
ST constr Date: null 1
Time: 00:00
Message: null
Also on a side note, I tried running my WorkOrder class and the arrayobjects that I preloaded are all null. I got these results before my menu was shown.
Code:
ST constr Date: null 1
Time: 00:00
Message: null
ST constr Date: null 0
Time: 00:00
Message: null
ST constr Date: null 0
Time: 00:00
Message: null
ST constr Date: null 0
Time: 00:00
Message: null
ST constr Date: null 0
Time: 00:00
Message: null
Select an Option
A)dd SystemTask
D)elete SystemTask
L)ist SystemTask
E)xit
|

March 11th, 2013, 04:53 PM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
|
What was printed by the println() methods called in the set... methods?
The print out shows that the constructor is NOT setting the variables from the args that are passed to it. Why?
|

March 11th, 2013, 05:10 PM
|
|
Contributing User
|
|
Join Date: Oct 2012
Posts: 31
Time spent in forums: 7 h 8 m 13 sec
Reputation Power: 1
|
|
Quote: | Originally Posted by NormR What was printed by the println() methods called in the set... methods?
The print out shows that the constructor is NOT setting the variables from the args that are passed to it. Why? |
Forgive me, but I don't quite understand what you mean by "what was printed out in the set methods". Could you please explain?
I don't think I'm printing out anything in those methods...or I haven't noticed that I've been doing that.
Also, I tried changing the parameters in my SystemTask constructor from:
Code:
public SystemTask(String month, int day, int hour, int minute,
String message) {
setDay(day);
setHour(hour);
setMinute(minute);
setMonth(month);
setMessage(message);
to this:
Code:
public SystemTask(String month, int day, int hour, int minute,
String message) {
setDay(day);
setHour(hour);
setMinute(minute);
setMonth(month);
setMessage(message);
and it almost works when I choose to display the preloaded arrays in my WorkOrders class.
Code:
Select an Option
A)dd SystemTask
D)elete SystemTask
L)ist SystemTask
E)xit
l
Date: Mar 4
Time: 21:30
Message: null
Date: Apr 1
Time: 17:00
Message: null
Date: May 6
Time: 10:45
Message: null
Date: Jun 3
Time: 09:15
Message: null
null
Select an Option
A)dd SystemTask
D)elete SystemTask
L)ist SystemTask
E)xit
It displays almost everything in my preset array slots except the message shows up as null.
|

March 11th, 2013, 05:14 PM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
Quote: | the message shows up as null. |
Trace the path from the call to the constructor to where the data is stored and on to where the value is picked up to display by the toString() method. Somewhere the code is not saving or accessing the variable correctly.
Use the println() method to print out the values at each step in the path.
|

March 11th, 2013, 08:13 PM
|
|
Contributing User
|
|
Join Date: Oct 2012
Posts: 31
Time spent in forums: 7 h 8 m 13 sec
Reputation Power: 1
|
|
Quote: | Originally Posted by NormR Trace the path from the call to the constructor to where the data is stored and on to where the value is picked up to display by the toString() method. Somewhere the code is not saving or accessing the variable correctly.
Use the println() method to print out the values at each step in the path. |
I did what you said and I figured out why my message showed up as null. My setMessage() method wasn't setting anything. It turns out it was just an empty method, so thank you
Now, I'm somewhat new to storing objects into arrays. How exactly would you do that? I don't know how to save my String into a slot of my array object while shifting the array.
Note: The project requirements state that I cannot use any sort method, I can only shift the array.
|

March 11th, 2013, 09:06 PM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
Quote: | storing objects into arrays. How exactly would you do that? |
Your program has code that does that:
Code:
arrayobject[0] = new SystemTask("Mar",4,21,30,"Backup Users");
arrayobject[1] = new SystemTask("Apr",1,17,00,"Upgrade Hard Drives");
arrayobject[2] = new SystemTask("May",6,10,45,"Virus Scan");
arrayobject[3] = new SystemTask("Jun",3,9,15,"Database Backup");
That code stores 4 objects of type: SystemTask in the array: arrayobject.
Shifting the array will involve using variables as indexes into the array and possibly saving the contents of an array element in a temp variable before storing it back in the array.
Code:
aVar = theArray[theIndex]; // copy element out of array into variable: aVar
theArray[anotherIndex] = someVar; // save variable's value in an array
|

March 11th, 2013, 11:11 PM
|
|
Contributing User
|
|
Join Date: Oct 2012
Posts: 31
Time spent in forums: 7 h 8 m 13 sec
Reputation Power: 1
|
|
Quote: | Originally Posted by NormR Your program has code that does that:
Code:
arrayobject[0] = new SystemTask("Mar",4,21,30,"Backup Users");
arrayobject[1] = new SystemTask("Apr",1,17,00,"Upgrade Hard Drives");
arrayobject[2] = new SystemTask("May",6,10,45,"Virus Scan");
arrayobject[3] = new SystemTask("Jun",3,9,15,"Database Backup");
That code stores 4 objects of type: SystemTask in the array: arrayobject.
|
So then how would I store whatever user input is entered into an array slot? It's an array of 20 SystemTask objects.
|

March 12th, 2013, 06:41 AM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
Quote: | how would I store whatever user input is entered into an array slot |
What kind of array is it? What kind of data does the user enter?
See post#12 for the assignment statement to store a value into an array.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|