Need Help With My Java Project

  • Thread starter Thread starter Chalupa
  • Start date Start date
C

Chalupa

Guest
Hello,
I am new in Java programming. I am currently having a hard time with my java project.
It's actually a small class project, It seems like I keep on forgetting to put something when I write this program, I tried to fix it several times, but still no luck, I am still having a hard time making my program run the way it's supposed to run. Can any of you guys here help me? If there's anyone of you here that can help me, just leave me your email, and I'll email the file to you so that you can read it and point out the mistakes to me.

Thanks.
 
I don't know java (much) but if the code's not too big, you could post it here, along with what's going wrong.
 
Yeah - throw it up on a website somewhere if it's big, or just here if it's small. I've been doing Java for a while, so might be able to help!
 
My Project

Here is what I am supposed to do:

Write a Java application by starting with project 1 and modifying it to read from the command line a sequence of names. These names will be used to create an array of boats (i.e., each name will be used to create an element in the array for a boat). Populate the array with instances of boats. Assign the member field with the name of the boat read in from the command line. Any boat name that starts with the letters B,C, and N will raise the sail; otherwise lower the sail (note: call member functions to get the job done). Print out the following on the standard output device.

Boat name
Where the sail is
Repeat for each boat in the array.




And here is what I have done so far (file attached). My question is: how do I make an array of boats (with the following names -> Wasabi, Chalupa, and Neptune) and get this program to work the way I was told to do? I have read my java textbook, tried it several times, but wasn't able to get it to run. I must have screwed up something. By the way, I am new in java, so if you see anything funny in my program, please don't laugh :o
 
Okay, your mistakes are (in order of appearence):

(1) SetName sets sailUp to true. From the description you gave; this should not happen.

(2) You have 2 copies of lowerSail()

(3) The piece of code
Code:
Boat[]	Boats = new Boat[3] ;
should be
Code:
Boat[]	Boats = new Boat[args.length] ;
considering the descrption you gave is for an arbitrarily long list of boats

(4) The piece of code
Code:
			Boats[0] = new Wasabi() ;
			Boats[1] = new Chalupa() ;
			Boats[2] = new Neptune() ;
should be
Code:
Boats[i] = new Boat();
because, (a) you shouldn't be creating 3 new boats each iteration of the loop, (b) you have no class named 'wasabi', 'chalupa', or 'neptune', I'm assuming you wanted to create new boat objects, (c) this should be done for an arbitrarily long list of boats.

(5) The piece of code
Code:
for (i = 0; i < 3; i++) {
should be
Code:
for (i = 0; i < args.length; i++) {
considering the descrption you gave is for an arbitrarily long list of boats

(6) The piece of code
Code:
			System.out.print("The sail is up ") ;
			Boats[i].whereIsTheSail() ;
should be
Code:
			System.out.print("    ") ;
			Boats[i].whereIsTheSail() ;
to maintain formatting.

-------------------------------------------------
So the corrected code would be
Code:
class Boat {

	String boatName ;
	boolean sailUp = false ;


	void setName(String name) {

		boatName = name ;
	}
	void printName() {

		System.out.println("This boat's name is " + boatName) ;
	}
	void goFast() {

		System.out.println("Raising the sail") ;
		sailUp = true ;
	}

	void goSlow() {

		System.out.println("Lowering the sail") ;
		sailUp = false ;
	}

	void raiseSail() {

		sailUp = true ;
	}
	void lowerSail() {

		sailUp = false ;

	}


	void whereIsTheSail() {

		if (sailUp) {
			System.out.println("The sail is up") ;
		} else {
			System.out.println("The sail is down") ;
		}
	}
}


public class Proj2 {


	public static void main(String args[]) {

		Boat[]	Boats = new Boat[args.length] ;
		char	firstChar ;
		int		i ;

		for (i = 0; i < args.length; i++) {

			Boats[i] = new Boat();
			Boats[i].setName(args[i]) ;
			firstChar = args[i].charAt(0) ;

			if ((firstChar == 'B') || (firstChar == 'C') || (firstChar == 'N')) {
				Boats[i].raiseSail() ;

			} else {
				Boats[i].lowerSail() ;
			}

		}

		for (i = 0; i < args.length; i++) {

			System.out.print("Boat number " + (i + 1) + " - \n") ;

			System.out.print("    ") ;
			Boats[i].printName() ;

			System.out.print("    ") ;
			Boats[i].whereIsTheSail() ;

		}

	}

}
 
Oh okay, but then how do I make an array of boats with different names? Let say I want to create three different boats (each with different name), where should I put the Boats' name?
 
You use the names of the boats as command line arguments, if you want 3 names you just put 3 arguments.

Example, using a command prompt (windows), or shell(unix/linux),
Code:
javac Proj2.java
java Proj2 Wasabi Chalupa Neptune
would produce the output
Code:
Boat number 1 - 
    This boat's name is Wasabi
    The sail is down
Boat number 2 - 
    This boat's name is Chalupa
    The sail is up
Boat number 3 - 
    This boat's name is Neptune
    The sail is up
----------------------------------------
If, however, you wanted to hardcode the names into the source, first (for simplicity) add a constructor to the boat object
Code:
Boat(String name){boatName = name;}
then remove the boat creation from the for loop, and before the loop put
Code:
Boat[0] = new Boat("Wasabi");
Boat[1] = new Boat("Chalupa");
Boat[2] = new Boat("Neptune");
 
why is it when I compile it --> javac Proj2.java, I keep on getting an error message that says ---> error: cannot read: Proj2.java ?
 
Either
(a) You didn't save the file as Proj2.java
(b) You aren't in the right directory.
 
Thanks Akrin, I really appreciate your help. I have been trying to figure out the solution to my problem for the past couple of weeks, with little success. And yet, you solved my problem just within minutes of my posting. I am impressed!

BTW, how long have you been using Java?
 
Started using Java about a year ago, been programming with a variety of other languages for 8 years.
 
I have just finished writing another project, I am now faced with the same problem, can't get it to run properly. I wonder what did I do wrong this time?


Here is what I was told to do:

Project 3 is devised to create an inheritance hierarchy of boats. Specifically, we will create two types of boats; race boat and sail boat. There will be a common interface between the two via an abstract class called Boat. There will be two derived classes, SailBoat and RaceBoat. Starting with project 2, we can modify it to have the Boat class be our super class. The methods to name the boat and print out the boat’s name should remain the same in the Boat class. The methods in the Boat Class, goSlow and goFast should be made abstract. The behavior for SailBoat goSlow and goFast will be the same as it was in project 2 Boat class. The RaceBoat class will implement goSlow and goFast by writing out “Throttle Back”, “Throttle Forward” respectively. For the SailBoat the state of the sail must be maintained. Likewise, for the RaceBoat the state of the throttle must be maintained. Method whereIsTheSail should be removed from the Boat class. It should be replace with an abstract method called whatIsBoatState. The RaceBoat will override this method by printing out the state of it’s throttle ; “Throttle Back”, “Throttle Forward”.
The SailBoat will override this method by printing out the state of it’s sail ; “Sail Up”, “Sail Down”.

Finally, write a Java application to read from the command line a sequence of names. These names will be used to create an array of sail boats or race boats (i.e., each name will be used to create a element in the array for a type of boat). Note the array will be an array of Boats (the super class). Populate the array with instances of sail boat or race boats. Assign the member field with the name of the boat read in from the command line. Any boat name that starts with the letters B,C, and N will be a sail boat; otherwise it will be a race boat. Any boat name that has it second letter in its name as letters A or E will make the boat go fast; otherwise the boat will go slow. (note: call member functions to get the job done). At the end of the program in main print out the following on the standard output device.

Boat name
State of boat
Repeat for each boat in the array.
 
Put the classes SailBoat and RaceBoat in "SailBoat.java" & "RaceBoat.java" respectively. Classes need to be in their own file. Once you do that it appears to fully meet the specifications you provided.
 
Chalupa, I am a java, c++ programer and I suggest that you start reading your data from an input file.

1-Start by reading the names of the boats
2-Save each boat into the array
3-loop

then do what you required to do. ! :)

its looks more pro than using args(arguments)

Edit: start using I/O files from now on. this is your next step.
 
Gorgon said:
Chalupa, I am a java, c++ programer and I suggest that you start reading your data from an input file.

1-Start by reading the names of the boats
2-Save each boat into the array
3-loop

then do what you required to do. ! :)

its looks more pro than using args(arguments)

Edit: start using I/O files from now on. this is your next step.
Given his description of what he has to do (use commandline args.) i don't think he'll be reading from a file. ;)

I do think that if he want's to expand his java knowledge I/O would be a good place to continue (if he hasn't already done I/O)
 
SLH said:
Given his description of what he has to do (use commandline args.) i don't think he'll be reading from a file. ;)

I do think that if he want's to expand his java knowledge I/O would be a good place to continue (if he hasn't already done I/O)

Thats what I meant. :cheers: ..............yeah I mean if he's a starter the next step for him is I/O
 
Guys, I made an error in class B, how do I fix it so that it runs? Thanks guys.

*File Attached*
 
The problem is that A's constructor is expecting an int passed to it.

When B's constructor is called it automatically tries to call As constructor, but it's not sure what to pass it.

To fix this either remove 'int a' from A's constructor, or explicitly pass something to A's constructor in B(), like this:
Code:
class B extends A {
    int x;
    B() {
        super(0); //explicitly passing an int to A()
        System.out.println(" class B");
    }

    public static void main (String [] args) {
        A a = new B();
    }
}
 
Hi,
I was wondering whether there are free softwares out there that we can use to write java other than notepad? Right now I am writing my programs only in microsoft notepad.
 
if your just looking for a 'notepad replacement' you could try metapad. it's pretty much just notepad with some more features.

if you mean specifically for coding, there is a program called 'textpad'. It's not actually free, but you can download a free 'trial' version. I've never actually used it, but that's what my CS prof. recommends. Assuming you have the Sun Java SDK installed, it will allow you to compile your program right from the menus. I guess he's been 'trial'-ing it for years.

if you can get ahold of 'em (some schools have free availability of educationally liscenced software) you could get an IDE, like borland, Visual Studio, or Sun One. Their editors are pretty nice also, with search and replace, keyword highlighting, auto formatting, and other nifty features.
 
How do I fix my class B so that it can compile without errors?
 
Edit: ok, first B should be an abstract class

and void F(){}
should be public void F(){}

and i dont know about you but i was always told to use caps for classes and not to us caps for method names, but thats just a style point.
 
Not to sound mean or anything but didn't you get a book for this class... This is pretty simple stuff that...

A) Your teacher must really suck or...

B) You didn't pay attention too...

but you seem to have the right attitude...you really want to learn this stuff and are not just asking for people to do your homework... keep up the good work...
 
Don't know if anyone is around anymore who posted about this...but apparently I have the same instructor as Chalupa and I'm stuck on Project 3 (a portion of it) as well.

The instructor tells us to create a new super class that is an abstract class (named Boat) then later states we should create an array of type "Boat". How can we create an array of type "Boat" when Boat is abstract? I must be missing something. I thought an abstract class could not be instantiated with "new"?
 
Back
Top