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

Thread: Help needed with java coding!!

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

    Default Help needed with java coding!!

    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.

    Driver code class:
    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:
    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; February 14th, 2012 at 06:56 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help needed with java coding!!

    the program, the console terminates after "Please enter the cost factor" question
    It looks like that is the last prompt for input.
    Where in the code is there any reading of input done after:
    costFactor= kbd.nextDouble();

    What do you expect to happen after the user responds to that question?

    i am unable to get the cost of the roadway.
    Are you saying that you don't have any System.out.println statements to print out the cost?

    Have you tried adding a System.out.println to show the cost?
    Last edited by Norm; February 14th, 2012 at 08:11 PM.

Similar Threads

  1. Java Applet Coding Help?
    By Drag01 in forum Java Applets
    Replies: 1
    Last Post: April 26th, 2012, 07:03 AM
  2. Replies: 1
    Last Post: June 28th, 2011, 07:34 AM
  3. Help me in java coding
    By smallmac in forum Java Theory & Questions
    Replies: 5
    Last Post: August 2nd, 2010, 09:50 AM
  4. Java coding help
    By Javalover1 in forum The Cafe
    Replies: 0
    Last Post: April 12th, 2010, 08:11 PM
  5. 15, Loves ICT, learning Java coding.
    By Sergant Mitch in forum Member Introductions
    Replies: 1
    Last Post: September 20th, 2008, 09:40 AM