I apologize but I am new to programming and I’m having difficulty coding a program. The original program is as follows:
import java.io.*;
import java.io.IOException;
import java.io.InputStreamReader;
class buildABoat{
String boatName; // Boat name
void buildABoat(){
String BoatName;
}
void nameTheBoat() {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("\n\nWhat should we name this vessel? \n\n");
this.boatName = br.readLine();
System.out.println("\n\nThe " + boatName + " is ready to sail!\n");
}
catch (IOException e) {
}
}
}
class proj1{
public static void main(String[] arg){
buildABoat boat1;
buildABoat boat2;
buildABoat boat3;
buildABoat boat4;
buildABoat boat5;
boat1 = new buildABoat();
boat2 = new buildABoat();
boat3 = new buildABoat();
boat4 = new buildABoat();
boat5 = new buildABoat();
boat1.nameTheBoat();
boat2.nameTheBoat();
boat3.nameTheBoat();
boat4.nameTheBoat();
boat5.nameTheBoat();
System.out.println("(Press ENTER to exit)");
try {
System.in.read();
}
catch (IOException e) {
return;
}
}
}
This produces the following:
What should we name this vessel?
Enterprise
The Enterprise is ready to sail!
What should we name this vessel?
Columbia
The Columbia is ready to sail!
What should we name this vessel?
Challenger
The Challenger is ready to sail!
What should we name this vessel?
Atlantis
The Atlantis is ready to sail!
What should we name this vessel?
Endeavor
The Endeavor is ready to sail!
(Press ENTER to exit)
I tried to change this to the following:
import java.io.*;
import java.io.IOException;
import java.io.InputStreamReader;
class buildABoat{
String boatName; // Boat name
void buildABoat(){
String BoatName;
}
void nameTheBoat() {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("\n\nWhat should we name this vessel? \n\n");
this.boatName = br.readLine();
System.out.println("\n\nThe " + boatName + " is ready to sail!\n");
}
catch (IOException e) {
}
}
}
class proj1{
public static void main(String[] arg){
Boat[] boat;
boat = new Boat[5];
for(int i = 0; i <= Boat.Length; i++){
nameTheBoat();
}
System.out.println("(Press ENTER to exit)");
try {
System.in.read();
}
catch (IOException e) {
return;
}
}
}
This of course produces the following error:
proj1.java:71: error: cannot find symbol
for(int i = 0; i <= Boat.Length; i++){
^
symbol: variable Length
location: class Boat
proj1.java:73: error: cannot find symbol
nameTheBoat();
^
symbol: method nameTheBoat()
location: class proj1
2 errors
What am I missing in the new program?
where you have
in your for() loop you need to have
The reasons are:
1)
nameTheBoat()is a method that only operates on objects of typeBoat. You are not giving it any object to work on.2)
boat[] = new Boat[5];initializes an Array object, but doesn’t create the 5 newBoats in theArrayobject. So you need to create each of those 5Boats before you can run aBoatmethod on them. Otherwise you’ll get a null pointer error.EDIT: and of course as others mentioned,
boat.lengthis the length of the arrayboat,boat.Lengthis wrong.Enjoy!