4 Replies - 3103 Views - Last Post: 10 September 2005 - 11:58 PM Rate Topic: -----

#1 cb153  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 21-June 05

stuck with code

Posted 09 September 2005 - 11:40 PM

I am new to java programming and I have been stuck for days on the following code. I know it should only be a few silly errors but I've no idea what and I am getting so frustrated! Could anybody help point out where I've made mistakes?

import java.util.*;
import java.text.*;

public class Assign2 {
private Vector v = new Vector(20);

public static void main(String [] args) {
Assign2 ass2 = new Assign2();
ass2.execute();
}

/**
* Processes the user input for the selection of a option from the menu
*/

public void execute() {
char sel = '0';
while (sel!='6') {
sel = menu();
switch(sel) {
case '1': add();
break;
case '2': register();
break;
case '3': find();
break;
case '4': list();
break;
case '5': remove();
break;
case '6': break;
default: System.out.println("Invalid entry");
}
}

}

/**
* Prints out the menu of options for the Small Boat registration program
*/

public char menu() {
char response;
System.out.println("\n" + "Small Boat Registration System");
System.out.println("-------------------------------");
System.out.println("\t" + "1." + "\t" + "Add a boat");
System.out.println("\t" + "2." + "\t" + "Register a boat");
System.out.println("\t" + "3." + "\t" + "Display a boat");
System.out.println("\t" + "4." + "\t" + "List all boats");
System.out.println("\t" + "5." + "\t" + "Remove a boat");
System.out.println("\t" + "6." + "\t" + "Exit");
System.out.print("\n" + "\t" + "Please select : ");
response = Keyboard.readString().charAt(0);
return response;
}

/**
* Menu option that adds a new boat to register
*/

public void add() {
System.out.print("\n" + "What type of boat: (P/S/R)");
char r = Keyboard.readString().charAt(0);
System.out.print("Enter Reg No : ");
String regno = Keyboard.readString();
System.out.print("\n" + "Length : ");
double length = Keyboard.readDouble();
System.out.print("\n" + "Passengers : ");
int passengers = Keyboard.readInt();
if (r == 'P') {
System.out.print("\n" + "Horsepower : ");
int horsepower = Keyboard.readInt();
SmallBoat power = new PowerBoat(regno, length, passengers, horsepower);
v.addElement(power);
register();
menu();
}
if (r == 'S') {
System.out.print("\n" + "Sail area : ");
double sailArea = Keyboard.readDouble();
System.out.print("\n" + "Masts : ");
int masts = Keyboard.readInt();
SmallBoat sail = new SailBoat(regno, length, passengers, sailArea, masts);
v.addElement(sail);
register();
}
if (r == 'R') {
SmallBoat row = new RowBoat(regno, length, passengers);
v.addElement(row);
register();
}
}

/**
* Menu option that registers a boat
*/
public void register() {
System.out.println("Enter Reg No : ");
String reg = Keyboard.readString();
boolean found = false;
for(int i = 0; i < v.size(); i++) {
String regno = ((SmallBoat)(v.elementAt(i))).regno;
if (reg.equals(regno)) {
found = true;
((SmallBoat)(v.elementAt(i))).currentReg = ((SmallBoat)(v.elementAt(i))).calcRegFee();
((SmallBoat)(v.elementAt(i))).regDate = new Date();
}

if (!found) {
System.out.println("Boat not found");
}
}
}


/**
* Menu option that locates a registered boat and prints its details
*/

public void find() {
System.out.println("Enter Reg No : ");
String reg = Keyboard.readString();
for (int i = 0; i < v.size(); i++) {
SmallBoat boat = ((SmallBoat)(v.elementAt(i)));
if (boat.regno.equals(reg)) {
System.out.println((SmallBoat)(v.elementAt(i)));
}
System.out.println("Boat not found");

menu();
}
}

/**
* Menu option that prints out a list of all registered boats
*/

public void list() {
for (int i = 0; i < v.size(); i++) {
SmallBoat b = ((SmallBoat)(v.elementAt(i)));
System.out.println(B);
}
menu();
}

/**
* Menu option that locates a registered boat and removes it
*/
public void remove() {
System.out.println("Enter Reg No : ");
String rg = Keyboard.readString();
for (int i = 0; i < v.size(); i++) {
SmallBoat sm = ((SmallBoat)(v.elementAt(i)));
if (sm.regno.equals(rg)) {
sm = (SmallBoat)(v.remove(i));
i--;
}
else
System.out.println("Boat not found");

}
menu();
}
}
// File: Keyboard.java

import java.io.*;

public class Keyboard {
static InputStreamReader isr = new InputStreamReader (System.in);
static BufferedReader br = new BufferedReader(isr);
public static String readString() {
String instr = "";
try {
instr = br.readLine();
}
catch (IOException e) {
System.out.println("Input error");
}
return instr;
}

public static String readString(String str) {
String instr = "";
try {
System.out.print(str);
instr = br.readLine();
}
catch(IOException e) {
System.out.println("Input Error");
}
return instr;
}

public static int readInt() {
String str = readString();
int number = Integer.parseInt(str);
return number;
}

public static double readDouble() {
String str = readString();
double number = Double.parseDouble(str);
return number;
}

public static char readChar() {
String line = " ";
try {
line = br.readLine();
}
catch (IOException e) {
System.out.println("Input error");
}
return line.charAt(0);
}
}
import java.util.*;
import java.text.*;

abstract class SmallBoat {

String regno;
double currentReg;
Date regDate;
double length;
int passengers;

/**
* Constructor to create an SmallBoat object.
* @param regno the registration number of the small boat
* @param length the length of the boat in metres
* @param passengers the number of passengers of the boat's specified capacity
*/

public SmallBoat(String regno,double length,int passengers) {
this.regno = regno;
this.length = length;
this.passengers = passengers;
}

/**
* Calculates the total of registration to pay
*/

abstract double calcRegFee();

public String getRegNo() {
return regno;
}

public void setRegNo(String regno) {
this.regno = regno;
}

public double getLength() {
return length;
}

public void setLength(double length) {
this.length = length;
}

public int getPassengers() {
return passengers;
}

public void setPassengers(int passengers) {
this.passengers = passengers;
}


public String toString() {
NumberFormat fmt = NumberFormat.getCurrencyInstance();
DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
String b = regno + ", " + df.format(regDate) + fmt.format(currentReg)
+ ", " + length + " m, " + passengers + " passengers, ";
return b;
}

}

import java.text.*;

public class SailBoat extends SmallBoat {
private double sailArea;
private int masts;

/**
* Constructor to create a SailBoat object.
* @param regno the registration number of the small boat
* @param length the length of the boat in metres
* @param passengers the number of passengers of the boat's specified capacity
* @param sailArea the total of the sail area in square metres
* @param masts the number of masts of the sail boat
*/

public SailBoat(String regno,double length,int passengers,double sailArea,int masts) {
super(regno, length, passengers);
this.sailArea = sailArea;
this.masts = masts;
}

/**
* Calculates the total of registration to pay
*/

public double calcRegFee() {
double str = 135 + 160 * masts + 12.50 * sailArea;
return str;
}

public String toString() {
NumberFormat fmt = NumberFormat.getCurrencyInstance();
DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
String sb = super.toString();
sb = "SailBoat" + regno + ", " + df.format(regDate) + ", " + fmt.format(currentReg) + ", "
+ ", " + length + " m, " + passengers + " passengers "
+ masts + " masts, " + sailArea + " sqm";
return sb;
}
}
import java.text.*;

public class RowBoat extends SmallBoat {

/**
* Constructor to create a RowBoat object.
* @param regno the registration number of the small boat
* @param length the length of the boat in metres
* @param passengers the number of passengers of the boat's specified capacity
*/

public RowBoat(String regno,double length,int passengers) {
super(regno, length, passengers);
}

/**
* Calculates the total of registration to pay
*/

public double calcRegFee() {
double t = 23 + 8.75 * length;
return t;
}

public String toString() {
NumberFormat fmt = NumberFormat.getCurrencyInstance();
DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
String rb = super.toString();
rb = "RowBoat " + regno + ", " + df.format(regDate) + ", " + fmt.format(currentReg) + ", "
+ ", " + length + " m, " + passengers + " passengers";
return rb;
}

}
import java.text.*;

public class PowerBoat extends SmallBoat {
private int horsepower;

/**
* Constructor to create a PowerBoat object.
* @param regno the registration number of the small boat
* @param length the length of the boat in metres
* @param passengers the number of passengers of the boat's specified capacity
* @param horsepower the horsepower of the power boat
*/

public PowerBoat(String regno,double length,int passengers,int horsepower) {
super(regno, length, passengers);
this.horsepower = horsepower;
}

/**
* Calculates the total of registration to pay
*/

public double calcRegFee() {
double total = 4.5 + (3.2 * horsepower);
return total;
}

public String toString() {
NumberFormat fmt = NumberFormat.getCurrencyInstance();
DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
String pb = super.toString();
pb = "PowerBoat " + regno + ", " + df.format(regDate) + ", " + fmt.format(currentReg) + ", "
+ ", " + length + " m, " + passengers + " passengers, "
+ horsepower + " hp";
return pb;
}
}

This is what I am supposed to get:

Sample output.
Small Boat Registration System
------------------------------
1. Add a boat
2. Register a boat
3. Display a boat
4. List all boats
5. Remove a boat
6. Exit
Please Select:1
What type of boat: (P/S/R)P
Enter Reg No: PB12
Length: 11.0
Passengers: 8
Horsepower: 45
Small Boat Registration System
------------------------------
1. Add a boat
2. Register a boat
3. Display a boat
4. List all boats
5. Remove a boat
6. Exit
Please Select:1
What type of boat: (P/S/R)S
Enter Reg No: SB209
Length: 18.0
Passengers: 12
Masts: 2
Sail Area: 38.6
Small Boat Registration System
------------------------------
1. Add a boat
2. Register a boat
3. Display a boat
4. List all boats
5. Remove a boat
6. Exit
Please Select:1
What type of boat: (P/S/R)R
Enter Reg No: RB001
Length: 6.0
Passengers: 4
For the output below, the options of the menu have not been displayed in order to save space
Small Boat Registration System
------------------------------
1. Add a boat
(etc)
Please Select:2
Enter Reg No: SB209
Small Boat Registration System
------------------------------
1. Add a boat
(etc)
Please Select:4
PowerBoat PB12, $189.00, 19/08/2005, 11.0m, 8 passengers, 45hp
SailBoat SB209, $937.50, 19/08/2005, 18.0m, 12 passengers, 2 masts, 38.6 sqm
RowBoat RB001, $75.50, 19/08/2005, 6.0m, 4 passengers,
Small Boat Registration System
------------------------------
1. Add a boat
(etc)
Please Select:3
Enter Reg No: rb001
Boat not found
Small Boat Registration System
------------------------------
1. Add a boat
(etc)
Please Select:3
Enter Reg No: RB001
RowBoat RB001, $75.50, 19/08/2005, 6.0m, 4 passengers,
Small Boat Registration System
------------------------------
1. Add a boat
(etc)
Please Select:5
Enter Reg No: SB209
Small Boat Registration System
------------------------------
1. Add a boat
(etc)
Please Select:4
PowerBoat PB12, $189.00, 19/08/2005, 11.0m, 8 passengers, 45hp
RowBoat RB001, $75.50, 19/08/2005, 6.0m, 4 passengers,
Small Boat Registration System
------------------------------
1. Add a boat (etc)
Please Select:6

But this is what I get:

----Hit any key to start.

Small Boat Registration System
-------------------------------
1. Add a boat
2. Register a boat
3. Display a boat
4. List all boats
5. Remove a boat
6. Exit

Please select : 1

What type of boat: (P/S/R)P
Enter Reg No : PB

Length : 12

Passengers : 4

Horsepower : 45
Enter Reg No :
?
Boat not found

Small Boat Registration System
-------------------------------
1. Add a boat
2. Register a boat
3. Display a boat
4. List all boats
5. Remove a boat
6. Exit

Please select : 1

Small Boat Registration System
-------------------------------
1. Add a boat
2. Register a boat
3. Display a boat
4. List all boats
5. Remove a boat
6. Exit

Please select : 1

What type of boat: (P/S/R)R
Enter Reg No : RB

Length : 6

Passengers : 2
Enter Reg No :
?
Boat not found
Boat not found

Small Boat Registration System
-------------------------------
1. Add a boat
2. Register a boat
3. Display a boat
4. List all boats
5. Remove a boat
6. Exit

Please select : 1

What type of boat: (P/S/R)S
Enter Reg No : SB

Length : 16

Passengers : 6

Sail area : 38

Masts : 1
Enter Reg No :
?
Boat not found
Boat not found
Boat not found

Small Boat Registration System
-------------------------------
1. Add a boat
2. Register a boat
3. Display a boat
4. List all boats
5. Remove a boat
6. Exit

Please select : 4
Exception in thread "main" java.lang.NullPointerException
at java.util.Calendar.setTime(Unknown Source)
at java.text.SimpleDateFormat.format(Unknown Source)
at java.text.SimpleDateFormat.format(Unknown Source)
at java.text.DateFormat.format(Unknown Source)
at SmallBoat.toString(SmallBoat.java:69)
at PowerBoat.toString(PowerBoat.java:41)
at java.lang.String.valueOf(Unknown Source)
at java.io.PrintStream.print(Unknown Source)
at java.io.PrintStream.println(Unknown Source)
at Assign2.list(Assign2.java:144)
at Assign2.execute(Assign2.java:38)
at Assign2.main(Assign2.java:20)

Is This A Good Question/Topic? 0
  • +

Replies To: stuck with code

#2 1lacca  Icon User is offline

  • code.rascal
  • member icon

Reputation: 44
  • View blog
  • Posts: 3,822
  • Joined: 11-August 05

Re: stuck with code

Posted 10 September 2005 - 05:02 AM

The null pointer exception occurs because you only initialize regDate in SmallBoat when you register it, so until that it is null. You have to fix the toString method in SmallBoat, so that it only tries to print regDate if its not null (the boat is registered).

If you have any other problems, please point them out 1 by 1, it makes them easier to solve for you.
Was This Post Helpful? 0
  • +
  • -

#3 cb153  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 21-June 05

Re: stuck with code

Posted 10 September 2005 - 07:20 PM

Thank-you so much for your answer, it has helped me to finally get it right!
Was This Post Helpful? 0
  • +
  • -

#4 skyhawk133  Icon User is offline

  • Head DIC Head
  • member icon

Reputation: 1877
  • View blog
  • Posts: 20,284
  • Joined: 17-March 01

Re: stuck with code

Posted 10 September 2005 - 10:05 PM

1lacca, thanks for helping cb153!
Was This Post Helpful? 0
  • +
  • -

#5 1lacca  Icon User is offline

  • code.rascal
  • member icon

Reputation: 44
  • View blog
  • Posts: 3,822
  • Joined: 11-August 05

Re: stuck with code

Posted 10 September 2005 - 11:58 PM

np:)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1