Stuck on Java code exercise

Stuck on Java code exercise

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

39 Replies - 2583 Views - Last Post: 17 March 2014 - 09:35 AM Rate Topic: -----

#1 JAVABLUEJHELP  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 11-March 14

Stuck on Java code exercise

Posted 11 March 2014 - 04:37 AM

the constructor should NOT have the date as a parameter,
but should set the data to the default 1/1/2013

I have tried many ways, but in this code there is no date class, which makes the only way i know to set this default date impossible, is there anyone who can help
Is This A Good Question/Topic? 0
  • +

Replies To: Stuck on Java code exercise

#2 mike73  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 250
  • View blog
  • Posts: 918
  • Joined: 24-April 10

Re: Stuck on Java code exercise

Posted 11 March 2014 - 04:47 AM

post the code(remember to use code tags)

This post has been edited by mike73: 11 March 2014 - 04:48 AM

Was This Post Helpful? 0
  • +
  • -

#3 JAVABLUEJHELP  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 11-March 14

Re: Stuck on Java code exercise

Posted 11 March 2014 - 04:52 AM

View Postmike73, on 11 March 2014 - 04:47 AM, said:

post the code(remember to use code tags)


There is no code to post, I had to make a new class called cars and then it tells me to declare some fields and now it asks me to define a default date inside the constructor.
Was This Post Helpful? 0
  • +
  • -

#4 peepsrct  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 151
  • Joined: 29-March 09

Re: Stuck on Java code exercise

Posted 11 March 2014 - 05:28 AM

Make a calendar instance, then use the .set(...) method to set the default.
Was This Post Helpful? 0
  • +
  • -

#5 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2386
  • View blog
  • Posts: 5,009
  • Joined: 11-December 07

Re: Stuck on Java code exercise

Posted 11 March 2014 - 05:35 AM

Quote

I had to make a new class called cars and then it tells me to declare some fields


You've already done this, right? This is the code you should post. In general, to set a field to default in a constructor, you do something like this:

public class Wibble {

  private String wobble;

  public Wibble() {
    wobble = "wibbley wobbly";
  }

}

Was This Post Helpful? 0
  • +
  • -

#6 JAVABLUEJHELP  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 11-March 14

Re: Stuck on Java code exercise

Posted 12 March 2014 - 03:42 AM

Thank you guys for your responses, was very helpful.

Each rally has a unique code, the venue, a start date and a number of days. There is a maximum number of places available for each rally. All members going on the rally are added to the rally list. Each rally has an entry cost code : 0 - 0, 1 - 10, 2 - 25, 3 - 35 and 4 - 40. For example: e.g. "MA1" at "Hatfield House" on 1/4/2014 lasts 3 days, has a cost code of 1 and a maximum of 20 places. The cost of attending a rally is the basic cost (as shown by its cost code) plus overnight accommodation at a cost of 30 per night, plus 20% VAT. So the cost of the above rally is
10 + (2 * 30) = 70, plus VAT = 70 + (70*0.2) = 70 + 14 = 84. Note: for a rally lasting 3 days, only 2 overnight accommodations are charged. A rally lasting 1 day, requires no overnight accommodation.


the constructor should have rally code, venue, cost code, number of days and maximum places as parameters

the constructor should NOT have the date as a parameter, but should set the data to the default 1/1/2013

the class should have an array costArray to provide a "look-up" table for the 5 entry costs (0-5)

the constructor should calculate and store the full cost using the "look up" table and the number of days

Define a mutator changeCost() which given a cost code and the new cost will update the relevant cost. For example: calling changeCost(2,27.50) should change the entry cost for cost code: 2 from 25 to 27.50

CAN ANYBODY HELP ME WITH THIS PLEASE, IM TOTALLY STUCK!.
Was This Post Helpful? 0
  • +
  • -

#7 mike73  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 250
  • View blog
  • Posts: 918
  • Joined: 24-April 10

Re: Stuck on Java code exercise

Posted 12 March 2014 - 04:13 AM

Make an attempt first, and then post your code.

Quote

You have basically laid out the steps you need to follow

the constructor should have rally code, venue, cost code, number of days and maximum places as parameters

the constructor should NOT have the date as a parameter, but should set the data to the default 1/1/2013

the class should have an array costArray to provide a "look-up" table for the 5 entry costs (0-5)

the constructor should calculate and store the full cost using the "look up" table and the number of days

Define a mutator changeCost() which given a cost code and the new cost will update the relevant cost. For example: calling changeCost(2,27.50) should change the entry cost for cost code: 2 from 25 to 27.50


Start at the first one and work your way down. then if you have trouble with a particular one we can help. No one will do it for you.
Was This Post Helpful? 0
  • +
  • -

#8 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3463
  • View blog
  • Posts: 15,668
  • Joined: 20-September 08

Re: Stuck on Java code exercise

Posted 12 March 2014 - 04:48 AM

Please don't post duplicates. I've edited the title of this to reflect the real problem(s)
Was This Post Helpful? 0
  • +
  • -

#9 JAVABLUEJHELP  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 11-March 14

Re: Stuck on Java code exercise

Posted 12 March 2014 - 04:57 AM

View Postmike73, on 12 March 2014 - 04:13 AM, said:

Make an attempt first, and then post your code.

Quote

You have basically laid out the steps you need to follow

the constructor should have rally code, venue, cost code, number of days and maximum places as parameters

the constructor should NOT have the date as a parameter, but should set the data to the default 1/1/2013

the class should have an array costArray to provide a "look-up" table for the 5 entry costs (0-5)

the constructor should calculate and store the full cost using the "look up" table and the number of days

Define a mutator changeCost() which given a cost code and the new cost will update the relevant cost. For example: calling changeCost(2,27.50) should change the entry cost for cost code: 2 from 25 to 27.50


Start at the first one and work your way down. then if you have trouble with a particular one we can help. No one will do it for you.


Thanks for the advice.

I Started 1 which i think is the following code :
FIELDS
private int RallyCode;
private String Venue;
private int CostCode;
private int NumberOfDays;
private int MaximumPlaces;
private Date DefaultDate;
private double FullCost;

Constructor
public Rally(int RCode, String Ven, int CCode, int NOfDays, int MPlaces)
{
RallyCode = RCode;
Venue = Ven;
CostCode = CCode;
NumberOfDays = NOfDays;
MaximumPlaces = MPlaces;
DefaultDate = new Date(1, 1, 2013);
FullCost = CostArray[CCode] + (NumberOfDays -1) * 30;
FullCost = FullCost * 1.2;

This is what i have so far for the first 4 questions.

for the Mutator one, i tried this, but it doesn't seem correct to me:

public void ChangeCost()
{
ChangeCost = ChangeCost;
}

If anyone could please help.
Was This Post Helpful? 0
  • +
  • -

#10 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3463
  • View blog
  • Posts: 15,668
  • Joined: 20-September 08

Re: Stuck on Java code exercise

Posted 12 March 2014 - 05:09 AM

Quote

post the code(remember to use code tags)

Was This Post Helpful? 2
  • +
  • -

#11 JAVABLUEJHELP  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 11-March 14

Re: Stuck on Java code exercise

Posted 12 March 2014 - 05:12 AM

Thanks for the advice.

I Started 1 which i think is the following code :
FIELDS
private int RallyCode;
private String Venue;
private int CostCode;
private int NumberOfDays;
private int MaximumPlaces;
private Date DefaultDate;
private double FullCost;



Constructor
public Rally(int RCode, String Ven, int CCode, int NOfDays, int MPlaces)
{
RallyCode = RCode; 
Venue = Ven; 
CostCode = CCode; 
NumberOfDays = NOfDays; 
MaximumPlaces = MPlaces; 
DefaultDate = new Date(1, 1, 2013); 
FullCost = CostArray[CCode] + (NumberOfDays -1) * 30;
FullCost = FullCost * 1.2;



This is what i have so far for the first 4 questions.

for the Mutator one, i tried this, but it doesn't seem correct to me:

public void ChangeCost() 
{
ChangeCost = ChangeCost;
}



If anyone could please help.
Was This Post Helpful? 0
  • +
  • -

#12 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2386
  • View blog
  • Posts: 5,009
  • Joined: 11-December 07

Re: Stuck on Java code exercise

Posted 12 March 2014 - 05:20 AM

Usually mutators take a new value and change the value of an existing variable. Something like this

public class SomeClass {
  
  // this is the existing variable
  private int numberOfHats;

  // The mutator takes an argument
  public void setHatCount(int newCount) {
    // Now update the existing variable
    //with the value that was passed in.
    numberOfHats = newCount;
  }

}

Was This Post Helpful? 0
  • +
  • -

#13 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3463
  • View blog
  • Posts: 15,668
  • Joined: 20-September 08

Re: Stuck on Java code exercise

Posted 12 March 2014 - 06:14 AM

Before you go any further, variables in Java begin lower case and are in camel case. Assignments to instance variables use the same variable name. See example

public class Example {
    private String foo;
    private int bar;

    public Example() {
    }

    public Example(String foo, int bar) {
        this.foo = foo;
        this.bar = bar;
    }

    public String getFoo() {
        return this.foo;
    }

    public int getBar() {
        return this.bar;
    }

    public void setFoo(String foo) {
        this.foo = foo;
    }

    public void setBar(int bar) {
        this.bar = bar;
    }

    public String toString() {
        return String.format("%s=%s,%s=%d", "foo", foo, "bar", bar);
    }
}


Was This Post Helpful? 0
  • +
  • -

#14 JAVABLUEJHELP  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 11-March 14

Re: Stuck on Java code exercise

Posted 14 March 2014 - 03:33 AM

View Postcfoley, on 12 March 2014 - 05:20 AM, said:

Usually mutators take a new value and change the value of an existing variable. Something like this

public class SomeClass {
  
  // this is the existing variable
  private int numberOfHats;

  // The mutator takes an argument
  public void setHatCount(int newCount) {
    // Now update the existing variable
    //with the value that was passed in.
    numberOfHats = newCount;
  }

}


Ahh right, so for this ChangeCost mutator
would it be like this?
public void ChangeCost(int newCost)
CostArray = newCost;


Was This Post Helpful? 0
  • +
  • -

#15 x68zeppelin80x  Icon User is offline

  • D.I.C Addict

Reputation: 130
  • View blog
  • Posts: 576
  • Joined: 07-March 09

Re: Stuck on Java code exercise

Posted 14 March 2014 - 03:43 AM

View PostJAVABLUEJHELP, on 14 March 2014 - 06:33 AM, said:

Ahh right, so for this ChangeCost mutator
would it be like this?
public void ChangeCost(int newCost)
CostArray = newCost;



Sorry, but you did not follow what cfoley and g00se said.

It should be:

int costArray; // I assume it's an int, but why???

public void changeCost(int newCost) {
    costArray = newCost;
}


Method names and non-final instance variables should use "mixedCase" (start with lowercase and then use an uppercase letter for each sequential word).

Also, I don't know why you are assigning a primitive integer to what is referred to as a "cost array"?

This post has been edited by x68zeppelin80x: 14 March 2014 - 03:46 AM

Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3