Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: Problem with School Assignment.

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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'.


    /**************************************************
    * 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();


    /**************************************************
    * 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.
    Attached Files Attached Files


  2. #2
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: Problem with School Assignment.

    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.
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  3. #3
    Junior Member
    Join Date
    Feb 2012
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

  4. #4
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: Problem with School Assignment.

    No, no, no; not on your ShipTest class, in your Ship class. 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.
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  5. #5
    Junior Member
    Join Date
    Feb 2012
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with School Assignment.

    OH my bizzle.

    your talking about my

     
    public Ship(String bName, double s)
    	{
    		name = bName;
    		setSpeed(s);
    	}

    your saying I should do a blank one like:

    public Ship()
    {
     
    }

  6. #6
    Junior Member
    Join Date
    Feb 2012
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with School Assignment.

    A Ha!!

    Your a genius!!!

  7. #7
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: Problem with School Assignment.

    Hahahaha, thank you! I'm gathering that it works? ;D

    By the way, sorry for the slow response; Minecraft is rather addicting.
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  8. #8
    Junior Member
    Join Date
    Feb 2012
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

  9. #9
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: Problem with School Assignment.

    Haha! 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!]
    Last edited by snowguy13; February 12th, 2012 at 09:33 PM.
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  10. #10
    Junior Member
    Join Date
    Feb 2012
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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?

  11. #11
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default 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
    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?
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  12. #12
    Junior Member
    Join Date
    Feb 2012
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

Similar Threads

  1. School Assignment AHH!
    By Europa in forum Loops & Control Statements
    Replies: 8
    Last Post: January 20th, 2012, 09:19 AM
  2. Problem with code for school assignment?
    By Mellisa315 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 16th, 2010, 09:36 PM
  3. Assignment problem.
    By minou13 in forum Java Theory & Questions
    Replies: 1
    Last Post: November 26th, 2010, 10:51 PM
  4. Java program for to implement supermarket menu
    By 5723 in forum AWT / Java Swing
    Replies: 1
    Last Post: April 14th, 2009, 03:14 AM
  5. How to use for loop for movement of points in a picture display?
    By Dman in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 8th, 2009, 09:19 AM