1 Attachment(s)
Problem with School Assignment.
Below is an attachment with two pages. "The UML diagram and instructions are on there also, so you can understand" *i need help with ShipTest.java, cause I can't get it to compile, mainly cause I haven't gotten much written cause I haven't complete Ship.java all the way.
So far, I have Ship.java written, but not 100% correctly, because I used bits an pieces from code I have written to build it up so it would compile, the returns are not exactly right.
The only real problem I am having with it, is the "double" 'timeToCrossEnglishChannel'.
Code java:
/**************************************************
* Name: Matt Spencer
* Assignment: A04
**************************************************/
public class Ship
{
//fields
private String name;
private double speed;
//constructors
public Ship(String bName, double s)
{
name = bName;
setSpeed(s);
}
//methods
public String getName()
{
return name;
}
public double getSpeed()
{
return speed;
}
public void setName(String newName)
{
name = newName;
}
public void setSpeed(double s)
{
if (s >= 0)
speed = s;
}
public double timeToCrossEnglishChannel(double s)
{
return speed * 2;
}
}
ShipTest.java is much worst, I can't figure out why it won't compile, it keeps putting an error on my "=" sign, for the line Ship myShip = new Ship();
Code java:
/**************************************************
* Name: Matt Spencer
* Assignment: A04
**************************************************/
import java.util.Scanner;
public class ShipTest
{
public static void main(String[] args)
{
Ship myShip = new Ship();
myShip.setName("Rob Roy");
}
}
thanks for any help.
Re: Problem with School Assignment.
Quote:
it keeps putting an error on my "=" sign, for the line Ship myShip = new Ship();
You're getting an error on this line because you haven't defined a constructor with no arguments. By default and for each class you make, Java makes a constructor that takes no arguments. However, when you define a constructor with arguments in your class, the default constructor is no longer created (such as in your Ship class). In your Ship class, the only valid constructor is one that takes two arguments. So, to fix this, you could either create a second constructor in your Ship class or fix your line of code:
Ship myShip = new Ship();
so that when you call the Ship() constructor you put in arguments.
Re: Problem with School Assignment.
Well it is pointing at the "new". So your saying I need a new constructor on my ShipTest page.
I have tried making a new constructor, but I just get more errors.
Re: Problem with School Assignment.
No, no, no; not on your ShipTest class, in your Ship class. :P The only constructor that you have in your Ship class is one that takes two arguments, but you want to create a new Ship with no arguments. To do this, you have to create a constructor in your Ship class that has no arguments listed in it. :)
Re: Problem with School Assignment.
OH my bizzle.
your talking about my
Code java:
public Ship(String bName, double s)
{
name = bName;
setSpeed(s);
}
your saying I should do a blank one like:
Re: Problem with School Assignment.
Re: Problem with School Assignment.
Hahahaha, thank you! :P I'm gathering that it works? ;D
By the way, sorry for the slow response; Minecraft is rather addicting. ;)
Re: Problem with School Assignment.
The first time i played minecraft I stayed up all night and played all the next day, I had fun building big walls, and made cool "yellow brick" roads that go everywhere.
Yeah I got the ShipTest to compile, and so I will work my way through some more.
The only other problem i had with Ship.java., was the return values for the set's and EnglishChannel.
The return values for the setName and setSpeed might not get my intended answers in the end, but the EnglishChannel is a doosy.
I need to have a "Speed", and based on that speed I need to translate it to Knots, and calculate how long it would take to get 21 miles(assuming your speed is a constant)
This is our first assignment where we had to do the "Class" and "ClassTest", in the past we have always had completed "Class" and then had to write the "ClassTests" from scratch.
Re: Problem with School Assignment.
Haha! :D Minecraft is pretty awesome.
So... Your problem is with the timeToCrossEnglishChannel() method. First of all, unless I'm mistaken, the PDF you provided said that this method won't take any arguments (which makes sense, because the Ship object stores the information for speed).
I don't remember the formula, but your PDF provided a conversion from knots to mph. Then, you'll need a little math:
rate = distance / time
If we rearrange that to solve for time (which we don't know; we do know distance -- 21 miles -- and rate -- the speed of the Ship object), we get this:
time = distance / rate
So, you need to convert your speed to mph, then divide the distance through the channel by the speed of the Ship.
As for you set methods, I don't see any issue. The return type you have coded is void, which is correct (set method's don't return anything, just set a value of an object's field). What is the problem?
[EDIT: I'm going to sleep for tonight. I'll check the thread tomorrow and offer more help if you still need it. Good luck!]
Re: Problem with School Assignment.
According to the paper, Paddle Streamer will got 21 miles / 6 knots = 3.5, according to output labeled, it will equal 3, so I will have to calculate to no decimals.
For the Hover Craft we got 21 miles / 60 knots = .35 , according to the output labeled, it will equal .3, so i will have to calculate to 1 decimal place.
Should I make speed a variable like S for the return of setSpeed, so that the EnglishCrossing will be like "return 21 / s;" and then later change the variable based on which ship?
Re: Problem with School Assignment.
No, you don't need to do that. Speed is a private variable of Ship, which means only the Ship class can directly access it (that's why we need getters and setters, so other classes can still call the values of a Ship object). This also means that ANY method in your ship class can reference speed simply by you typing "speed," as you did in your getSpeed and setSpeed methods.
For example, look at this quick class
Code Java:
public class Numbers
{
private int numA;
private int numB; //private, only THIS class can directly reference them
//constructors, getters, setters; you know what they look like :P
//Here's an example of a non-getter-setter method
//like your English Channel method
public int getSum()
{
return (numA + numB);
//Note, I can still just directly use "numA" and "numB"
//without any special set or get calls
}
}
Hopefully that makes things a little more clear?
Re: Problem with School Assignment.
OH, thanks man. Yeah I have been sick this whole week, so I didn't get on the internet much.
But I think I am getting closer. I just have the hardest time writing code that I haven't before. Especially since I haven't got much experience.
I keep track of all the code I write, so I can look at them for reference. Each of my assignments end up being fully new and harder than ever.