Results 1 to 9 of 9
Like Tree1Likes
  • 1 Post By santa

Thread: Problem with Coding Java Class

  1. #1
    Bentino is offline Member
    Join Date
    Feb 2012
    Posts
    17
    Rep Power
    0

    Default Problem with Coding Java Class

    hello everyone,

    I am having an issue with getting my java class coding to complete the task.
    Here it is: I'm coding a a Java class called Roadway that meets the following UML specification:

    Roadway
    -length : double
    -numLanes : int
    -laneWidth : double
    -costFactor : double

    +Roadway(initLength:double, initLanes: int)
    +setCostFactor(factor:double):void
    -surfaceArea():double
    +printCost():void

    The methods in the above should do the following:
    • the constructor should give initial values to the length and numLanes attributes from the
    corresponding parameters, and give the laneWidth attribute the value 8.5.
    • the setCostFactor method should set the costFactor attribute to the method’s input
    parameter value.
    • the surfaceArea method should calculate and return the surface area of the road. Note that
    this could be calculated as the product of the number of lanes in the road times the width of
    one lane times the length of the road.
    • the printCost method should print the cost of the road to the screen. You MUST utilize the
    surfaceArea method to accomplish this. Note that the cost of the road is just its surface
    area times its cost factor.

    this is the driver code class:
    Java Code:
    public class RoadwayDriver
    {
    /*
    Note that this driver class does not have any attributes;
    It merely uses object(s) of another class as needed
    */
    public static void main(String args[])
    {
    int myRoadNumLanes; // how wide is our road (in lanes) ?
    double myRoadLength, // how long is our road (in feet) ?
    costFactor; // how much does a square foot cost?
    // we’ll be reading fromt he keyboad, build a Scanner accordingly
    Scanner kbd = new Scanner(System.in);
    
    // prompt for and get the road length
    System.out.print("Please enter the length of the new road:");
    myRoadLength = kbd.nextDouble();
    
    // prompt for and get the number of lanes in the road
    System.out.print("Please enter the number of lanes for the new road:");
    myRoadNumLanes = kbd.nextInt();
    
    // prompt for and get the cost per square foot
    System.out.print("Please enter the cost factor:");
    costFactor= kbd.nextDouble();
    
    // build up a roadway object using values read in
    Roadway myRoad = new Roadway(myRoadLength, myRoadNumLanes);
    
    // set the roadway’s cost factor from the value read in
    myRoad.setCostFactor(costFactor);
    
    // print out the roadway’s cost.
    myRoad.printCost();
    }
    }
    Java Code:
    Java Code:
    public class Roadway 
    {
    	private double length;
    	private int numLanes;
    	private double laneWidth;
    	private double costFactor;
    	private double printCost;
    	
    public Roadway(double initLength, int initLanes)
    {     length = initLength;
          numLanes = initLanes;
          laneWidth = 8.5;
    }
    
    public void costFactor(double factor)
    {     costFactor = factor;
    }
    
    public double getCostFactor()
    {     return costFactor;
    }
    
    
    public void setCostFactor(double costFactor)
    {     this.costFactor = costFactor;
    }
    
    private double surfaceArea()
    {     double surfaceArea = length * numLanes * laneWidth;
          return surfaceArea;
    }
    
    public void printCost()
    {     printCost = (surfaceArea() * costFactor);
    }
    
    public double getPrintCost() 
    {     return printCost;
    }
    
    public void setPrintCost(double printCost) 
    {     this.printCost = printCost;
    }
    when i run the program, the console terminates after "Please enter the cost factor" question so i am unable to get the cost of the roadway.
    if anyone could help me as soon as possible, i would really appreciate.
    thanks
    Last edited by Bentino; 02-15-2012 at 12:50 AM.

  2. #2
    santa's Avatar
    santa is offline Senior Member
    Join Date
    Nov 2009
    Location
    Sweden
    Posts
    208
    Rep Power
    5

    Default Re: Problem with Coding Java Class

    If you put code tags on your code, it will be easier to read and people will be willing to help =)

  3. #3
    Bentino is offline Member
    Join Date
    Feb 2012
    Posts
    17
    Rep Power
    0

    Default Re: Problem with Coding Java Class

    Is that a bit easier to read?

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: Problem with Coding Java Class

    Don't double post the same question. Your other thread has been closed.

    Please go through the Forum Rules.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    santa's Avatar
    santa is offline Senior Member
    Join Date
    Nov 2009
    Location
    Sweden
    Posts
    208
    Rep Power
    5

    Default Re: Problem with Coding Java Class

    Quote Originally Posted by Bentino View Post
    Is that a bit easier to read?
    No, just put code tags .. =)

  6. #6
    Bentino is offline Member
    Join Date
    Feb 2012
    Posts
    17
    Rep Power
    0

    Default Re: Problem with Coding Java Class

    db

    my mistake, I will remember that for future reference
    thanks

  7. #7
    Bentino is offline Member
    Join Date
    Feb 2012
    Posts
    17
    Rep Power
    0

    Default Re: Problem with Coding Java Class

    santa

    the code tags are in.

    thanks

  8. #8
    santa's Avatar
    santa is offline Senior Member
    Join Date
    Nov 2009
    Location
    Sweden
    Posts
    208
    Rep Power
    5

    Default Re: Problem with Coding Java Class

    when i run the program, the console terminates after "Please enter the cost factor" question so i am unable to get the cost of the roadway.
    Well it terminates because it has done whats its supposed to do. The print cost does not print anything ... put a system.out.println in it and it will display the cost.
    Bestsanchez likes this.

  9. #9
    Bentino is offline Member
    Join Date
    Feb 2012
    Posts
    17
    Rep Power
    0

    Default Re: Problem with Coding Java Class

    Santa

    I shall do that yeah!

    thank you for the help!

Similar Threads

  1. Help with Java coding assignment "the fan class"
    By broo7198 in forum New To Java
    Replies: 27
    Last Post: 08-09-2011, 09:50 AM
  2. The problem with the coding xml
    By torsion in forum CLDC and MIDP
    Replies: 0
    Last Post: 06-05-2010, 03:52 AM
  3. Coding Problem
    By mfaulhaber in forum New To Java
    Replies: 2
    Last Post: 11-16-2009, 11:44 PM
  4. Help On Coding problem
    By mandrake446 in forum New To Java
    Replies: 3
    Last Post: 12-08-2007, 07:01 AM
  5. Problem in my coding
    By one198 in forum New To Java
    Replies: 9
    Last Post: 08-09-2007, 10:07 AM

Posting Permissions

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