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 6 of 6

Thread: Problem with School Assignment.

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

    Default Problem with School Assignment.

    n/a
    Attached Files Attached Files
    Last edited by SaltSlasher; June 24th, 2024 at 03:10 AM.


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

  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.

    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.

  5. #5
    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.

  6. #6
    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.

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