Results 1 to 5 of 5

Thread: Please help

  1. #1
    wyldecut is offline Member
    Join Date
    Oct 2010
    Posts
    2
    Rep Power
    0

    Default Please help

    I'm stuck on a few questions for my homework and was hoping someone could please help me.

    1.Revision of Declarations
    Write definitions of the following:
    A variable/field called name of type String


    A variable/field of type int called age


    A variable/field of type String called code


    A variable/field called credits of type int



    2.Accessor and Mutator Methods
    Suppose that the class Pet has a variable/field called name that is of type String.

    Write an accessor method getName() that returns the value of name
    Enter your answer in this box
    public String getName() { return name; }

    Write a mutator method setName(), with a return type of void that may be used to change the value of name when the pet changes owners.




    3.Conditional statements
    Edit the TicketMachine class constructor that takes a single parameter that specifies the price.
    If the value passed to the TicketMachine constructor is negative it should set the value to (the arbitrary value of) 10 and output the message

    Price cannot be negative: Setting it to 10

    to the Terminal Window
    Test your implementation by trying to create TicketMachines with positive and zero values (accept), and with negative values (reject).






    4.Mutator returning a value
    Add a new method emptyMachine() to the TicketMachine class that is designed to simulate emptying the machine of money
    It should both return the previous value in total, and reset the value of total to zero.





    5.Revision of constructor definition
    Write out a constructor for a class called Module. The constructor should take a single parameter of type String called moduleCode. the body of the constructor should assign the value of its parameter to the variable code
    You should not include the definition of code, just the text of the constructor






    6.Revision of print method definition
    Write a method called printDetails() for a class with an instance variable/attribute of type String called name. You can assume that name has already been instantiated (e.g. to "Freda"). The printDetails() method should print out a String of the form "The name of this person is " followed by the value of name. For instance, if name were "Helen" the method would print

    The name of this person is Helen



    Many thanks in advance!

  2. #2
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Can we see what you have attempted?

  3. #3
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Thanks for dumping your homework in the forum here! Yeah, you'll go far!

  4. #4
    wyldecut is offline Member
    Join Date
    Oct 2010
    Posts
    2
    Rep Power
    0

    Default

    1.
    private String name()
    private int age()
    private String code()
    private int credits()

    2.public String getName()
    {
    return name;
    }

    and

    public void changeName(String replacementName)
    {
    name = replacementName;
    }


    5.{
    code = moduleCode
    }

    6.public void prompt()
    {
    System.out.println("The name of this person is " +name + "")

    name = ();
    }

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,368
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by wyldecut View Post
    1.
    private String name()
    private int age()
    private String code()
    private int credits()
    Have you studied the basic syntax of Java? That is not how you define variables; that looks like a half baked definition of a method. Study your book(s) some more.

    kind regards,

    Jos

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •