Notices
Computer & Technology Related Post here for help and discussion of computing and related technology. Internet, TVs, phones, consoles, computers, tablets and any other gadgets.

Java Class Help?

Thread Tools
 
Search this Thread
 
Old 21 December 2002, 07:32 PM
  #1  
Scooby Snax
Scooby Regular
Thread Starter
 
Scooby Snax's Avatar
 
Join Date: Apr 2002
Posts: 630
Likes: 0
Received 0 Likes on 0 Posts
Post

Hi, I have a very small problem with using classes for the first time. I am currently making a railway timetable program which takes a company name, train number, departure time and arrival time. I have done this by designing Train and Timetable classes and using 2 other given to me called DerbyIO and Time.

I got the programme to work up until the point of using the time class to get the programme to work out the 24hr clock. I decided to get the clock part of the programme to work on it own which i did fine. I have had trouble putting the two together. If anyone could spare me a few mins to see where i am going wrong i would be very gratefull.

Below is all the code!

DERBYIO CLASS
import javax.swing.*;
import java.util.StringTokenizer;

public class DerbyIO
{
public static int getInt(String message) {
int x, h;
StringTokenizer st = new StringTokenizer(message, "\n");
h = st.countTokens();
String s;
if (h == 1) {
s = JOptionPane.showInputDialog(message);
}
else {
JEditorPane jta = new JEditorPane("text/plain",message);
jta.setEditable(false);
s = JOptionPane.showInputDialog(jta);
}
try {
x = Integer.parseInt(s);
}
catch (Exception e) {
x = 0;
}
return x;
}

public static double getDouble(String message) {
double x;
int h;
StringTokenizer st = new StringTokenizer(message, "\n");
h = st.countTokens();
String s;
if (h == 1) {
s = JOptionPane.showInputDialog(message);
}
else {
JEditorPane jta = new JEditorPane("text/plain",message);
jta.setEditable(false);
s = JOptionPane.showInputDialog(jta);
}
try {
x = Double.parseDouble(s);
}
catch (Exception e) {
x = 0;
}
return x;
}

public static float getFloat(String message) {
float x;
int h;
StringTokenizer st = new StringTokenizer(message, "\n");
h = st.countTokens();
String s;
if (h == 1) {
s = JOptionPane.showInputDialog(message);
}
else {
JEditorPane jta = new JEditorPane("text/plain",message);
jta.setEditable(false);
s = JOptionPane.showInputDialog(jta);
}
try {
x = Float.parseFloat(s);
}
catch (Exception e) {
x = 0;
}
return x;
}

public static String getString(String message) {
int h;
StringTokenizer st = new StringTokenizer(message, "\n");
h = st.countTokens();
String s;
if (h == 1) {
s = JOptionPane.showInputDialog(message);
}
else {
JEditorPane jta = new JEditorPane("text/plain",message);
jta.setEditable(false);
s = JOptionPane.showInputDialog(jta);
}
return s;
}

public static void showMessage(String message) {
int x, h, w = 0;
StringTokenizer st = new StringTokenizer(message, "\n");
h = st.countTokens();
String s;
if (h == 1) {
JOptionPane.showMessageDialog(null, message);
}
else {
JEditorPane jta = new JEditorPane("text/plain", message);
jta.setEditable(false);
JOptionPane.showMessageDialog(null,jta);
}
}
}


TIME CLASS
public class Time
{
private int minutes; // time in minutes from midnight

public Time() {
// default constructor - set to midnight
minutes = 0;
}

public Time(int minutes) {
// construct a Time from a number of minutes after midnight
this.minutes = minutes % (24 * 60);
}

public Time(int hours, int minutes) {
// construct a Time from hours and minutes
this.minutes = (hours * 60 + minutes) % (24 * 60);
}

public void setTime(int hours, int minutes) {
this.minutes = (hours * 60 + minutes) % (24 * 60);
}

public int getMinutes() {
return minutes;
}

public Time add(Time t) {
// add this Time to Time t
int minutes = this.minutes + t.minutes;
return new Time(minutes);
}

public Time subtract(Time t) {
// subtract Time t from this Time
int minutes = this.minutes - t.minutes;
return new Time(minutes);
}

public boolean equals(Time t) {
// check if this Time and Time t are equal
if (this.minutes == t.minutes) return true;
return false;
}

public int compare(Time t) {
// compare this Time with Time t
if (this.minutes == t.minutes) return 0;
if (this.minutes > t.minutes) return 1;
return -1;
}

public String toString() {
// convert the Time to a String
String time = "";
int hours = minutes / 60;
int mins = minutes % 60;
time = Integer.toString(hours) + ":";
if (mins < 10) time = time + "0";
time = time + Integer.toString(mins);
return time;
}

public static Time parseTime(String s) throws NumberFormatException {
String a = "", b = "";
s = s.trim();
int i = 0, x = 0, y = 0;
// look for the ':' symbol in the String
while (i < s.length() && s.charAt(i) != ':') {
i++;
}
if (i == s.length() || i == 0 || i > 2){
// if there isn't one or it's in the wrong place ...
NumberFormatException ee = new NumberFormatException();
// throw a NumberFormatException to the next level
throw ee;
}
// split into hours and minutes
a = s.substring(0,i);
b = s.substring(i+1, s.length());
// convert to numbers
try {
x = Integer.parseInt(a);
y = Integer.parseInt(b);
}
catch (NumberFormatException e) {
throw e; // NumberFormatException thrown to next level
}
// return the Time object
return new Time(x, y);
}
}


TIMETABLE CLASS
import DerbyIO;
import Time;

public class Timetable
{
private static String trainName;
// create 20 empty trains
private static Train[] theAccounts = new Timetable[20];
private static int counter = 0;

public Timetable(String name){
trainName = name;
}

public String getTrainName() {
return trainName;
}

public static boolean createAccount(String name, long acno, long depTime, long arrTime) {
// create a new account at the first empty space (if there is one)
if (counter == 20) return false;
theAccounts[counter] = new Timetable(name, trno, depTime, arrTime);
counter++;
return true;
}

public static boolean deleteAccount(long trno) {
// search for this Train
int i = 0;
while(i < counter && theAccounts[i].getAccountNumber() != trno) {
i++;
}
if (i == counter) return false;
while (i < counter-1) {
theAccounts[i] = theAccounts[i+1];
i++;
}
theAccounts[counter] = null;
counter--;
return true;
}

public static String getDetails(long trno) {
// search for this Train
int i = 0;
while(i < counter && theAccounts[i].getAccountNumber() != trno) {
i++;
}
if (i == counter) return "No such account"; // failed to find it
// returns the train details
return theAccounts.toString();
}

public static void main(String[] args)
{
trainName = "Railtrack";
String option = " ";
while (!option.equals("Q") && !option.equals("q")) {
option = DerbyIO.getString("a) Add a train\nb) Delete a train\nc) Get account details\nq) Quit");
// create a new train and initialise it
if (option.equals("a") || option.equals("A")) {
String trname = DerbyIO.getString("Train Company:");
long trno = (long) DerbyIO.getInt("Train Number:");

Time t1 = new Time(); // create a Time object
Time t2 = new Time(), t3; // and two more
String s = DerbyIO.getString("Enter the time");
String s2 = DerbyIO.getString("Enter a 2nd time");
try {
t1 = Time.parseTime(s2);
t2 = Time.parseTime(s); // convert this String to a Time
}
catch (NumberFormatException e) {
System.err.print(e);
System.exit(0);
}
String depTime = (s2);
String arrTime = (s);
if (createAccount(trname, trno, depTime, arrTime))
DerbyIO.showMessage("Account Created OK");
else
DerbyIO.showMessage("Unable to create account");
}
// delete an existing account
if (option.equals("b") || option.equals("B")){
long trno = (long) DerbyIO.getInt("Train Number:");
if (deleteAccount(trno))
DerbyIO.showMessage("Train Deleted OK");
else
DerbyIO.showMessage("Unable to delete train details");
}

if (option.equals("c") || option.equals("C")){
long trno = (long) DerbyIO.getInt("Train Number:");
String result = getDetails(trno);
DerbyIO.showMessage(result);
}
}
DerbyIO.showMessage("Thank you for using " + trainName);
System.exit(0);
}
}


TRAIN CLASS
public class Train
{
// class attributes - these are normally 'private'
private String company;
private long trainNumber;
private String departureTime;
private String arrivalTime;

// class methods - these are normally 'public'
public Train(String trName, long trNo, String depTime, String arrTime) {
setDetails(trName, trNo, depTime, arrTime);
}

public void setDetails(String trName, long trNo, String depTime, String arrTime) {
company = trName;
trainNumber = trNo;
departureTime = depTime;
arrivalTime = arrTime;
}

public String getcompany() {
return company;
}

public long getTrainNumber() {
return trainNumber;
}

public String getdepartureTime() {
return departureTime;
}

public String getarrivalTime () {
return arrivalTime;
}

public String toString() {
String result = "Account holder: " + company +
"\nAccount Number: " + Long.toString(trainNumber) +
"\nDeparture Time: " + Long.toString(departureTime) +
"\nArrival Time: " + Long.toString(arrivalTime);
return result;
}
}

If anyone can help i would be very grateful!
Thanx, Mark
Old 21 December 2002, 07:57 PM
  #2  
super_si
Scooby Regular
 
super_si's Avatar
 
Join Date: Feb 2002
Location: Lurkin Somewhere
Posts: 7,951
Likes: 0
Received 0 Likes on 0 Posts
Post

http://www.cs.ncl.ac.uk

Look for 01-2002 CSC131 + 132.

My Java modules, Sorry i havent looked at java in a while

Si
Old 21 December 2002, 08:27 PM
  #3  
michael_clarkson
Scooby Regular
 
michael_clarkson's Avatar
 
Join Date: Jan 2001
Posts: 253
Likes: 0
Received 0 Likes on 0 Posts
Post

Can you outline the problems your seeing ?
Old 21 December 2002, 08:46 PM
  #4  
dharbige
Scooby Regular
 
dharbige's Avatar
 
Join Date: Feb 2001
Posts: 845
Likes: 0
Received 0 Likes on 0 Posts
Post

Is this some kind of coursework?

If it is, may I suggest that the reason for being set it is for you to figure it out for yourself, rather than getting other people to do it for you.

Your first step may be to actually try to compile the code. It's full of simple syntax errors, incorrect variable names.
Old 21 December 2002, 10:29 PM
  #5  
ChrisS/P1
Scooby Regular
 
ChrisS/P1's Avatar
 
Join Date: Feb 2002
Location: Newcastle
Posts: 459
Likes: 0
Received 0 Likes on 0 Posts
Post

Can't be ****'d to read it all. What was the error message? Incompatible types? Incorrect pararemter passing? The list is endless. Be more specific about the the error, and I'll have a go.
Old 21 December 2002, 10:35 PM
  #6  
ChrisS/P1
Scooby Regular
 
ChrisS/P1's Avatar
 
Join Date: Feb 2002
Location: Newcastle
Posts: 459
Likes: 0
Received 0 Likes on 0 Posts
Post

For gods sake, why can't people indent their code properly. It would make it slightly readable.
Old 21 December 2002, 10:44 PM
  #7  
ChrisS/P1
Scooby Regular
 
ChrisS/P1's Avatar
 
Join Date: Feb 2002
Location: Newcastle
Posts: 459
Likes: 0
Received 0 Likes on 0 Posts
Post

Had a look, and I suggest you look at the rumber of parameters you are passing to the Time function. The Call dosen't match the prototype (said the old git).

Trending Topics

Old 22 December 2002, 02:43 PM
  #8  
Scooby Snax
Scooby Regular
Thread Starter
 
Scooby Snax's Avatar
 
Join Date: Apr 2002
Posts: 630
Likes: 0
Received 0 Likes on 0 Posts
Arrow

Hello, It is a bit of uni work i have to do but i would not ask for help if i did not need it.

Sorry about putting all the code up with so many stupid errors!

I have got it all working now apart from the inputting of the time in 24hr format.

What i need to change is the following:

long depTime = (long) DerbyIO.getInt("Departure Time");
long arrTime = (long) DerbyIO.getInt("Arrival Time");

The programme uses the above to input the time as a number and not as a real 24 time. I have got what i want to work but putting them
together just does not work.

Time t1 = new Time(); // create a Time object
Time t2 = new Time(), t3; // and two more
String s = DerbyIO.getString("Enter the time");
String s2 = DerbyIO.getString("Enter a 2nd time");
try {
t1 = Time.parseTime(s2);
t2 = Time.parseTime(s); // convert this String to a Time
}
catch (NumberFormatException e) {
System.err.print(e);
System.exit(0);
}

Does anyone have any ideas how i can use the 2nd bit of code in place of the first bit?

Thank you
Related Topics
Thread
Thread Starter
Forum
Replies
Last Post
InTurbo
Other Marques
20
08 October 2015 08:59 PM
williamw1987
ScoobyNet General
0
08 September 2015 12:15 PM
AWD
ScoobyNet General
42
19 April 2001 10:40 AM
S
Non Scooby Related
4
26 January 2001 10:21 PM



Quick Reply: Java Class Help?



All times are GMT +1. The time now is 03:41 AM.