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: Program returning wrong values.

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Program returning wrong values.

    After I compile the following and try to run it, it prints out the wrong values for the customer variable and cost variable.



    /** @author CCochran */
    /** @version Java Version 6 Update 30 */



    /** Creates Viewer class which represents a customer who purchased a pay-per-view movie */


    public class Viewer
    {

    /** Instance field for customer */
    private String Customer;

    /** Instance field for moviecode */
    private String moviecode;


    /** Instance field for cost */
    private double cost;

    /** Default constructor for Viewer class */
    public Viewer()
    {

    Customer = " ";

    moviecode = " ";

    cost = 0;

    }


    /** Paramterized constructor for viewer class @param acustomer mcode */

    public Viewer( String Customer, String moviecode)
    {

    this.Customer = this.Customer;

    this.moviecode = moviecode;

    this.cost = cost;

    computeCost();
    }


    /** Method that returns the name of the customer

    @return customer name */

    public String getCustomer()
    {

    return Customer;
    }

    /**Method that returns the moviecode

    @return movie code */

    public String getMovieCode()
    {

    return moviecode;

    }

    /**Method that returns the cost of the movie

    @return the cost */

    public double getCost()
    {

    return cost;

    }

    /** Method that computes the cost of the movie */
    private double computeCost()
    {


    double mCost = 3.95;
    char mc = ' ';
    char mCode = ' ';

    mCode = moviecode.charAt(0);
    mc = moviecode.charAt(0);



    switch(mc)
    {

    case 'A' : cost = mCost * 1.50; break;

    case 'B' : cost = mCost * 1.36; break;

    case 'C' : cost = mCost * 1.26; break;

    case 'D' : cost = mCost * 1.06 ; break ;

    case 'F' : cost = mCost * 1.00; break;

    default : System.out.println("Invalid code ");
    cost = 9999.99; }

    if( mCode == 'N')
    {

    cost = mCost + 2.50;

    }

    else
    {

    cost = 9999.99;

    }

    if(mCode == 'R')
    {

    cost = mCost + 1.00;

    }

    else
    {

    cost = 9999.99;

    }

    return cost;

    }

    /** Method that takes in a new moviecode and updates the cost and moviecode variables
    @param newmoviecode */
    public void updateMovieCode(String newmoviecode)
    {

    newmoviecode = moviecode;

    computeCost();


    }

    /** Method that returns a string with the customer, moviecode, and cost variables
    @return string */
    public String toString()
    {

    return (Customer + " " + moviecode + " " + cost) ;

    }

    }

    Tester class : import javax.swing.JOptionPane;

    /** Class that tests the methods of the viewer class */
    public class ViewerTest
    {
    /** Test methods of viewer class */
    public static void main(String[] args)
    {



    Viewer view1 = new Viewer("John Smith", "A");


    System.out.println(view1.toString());


    Viewer view2 = new Viewer("Chris", "B");

    System.out.println(view2.toString());

    Viewer view3 = new Viewer(" Darius", "C");

    System.out.println(view3.toString());

    Viewer view4 = new Viewer("Cheryl", "D");

    System.out.println(view4.toString());



    Viewer view5 = new Viewer("Christine","AN");

    System.out.println(view5.toString());

    Viewer view6 = new Viewer("Mark", "AR");

    System.out.println(view6.toString());

    Viewer view7 = new Viewer("Michelle"," BN");

    System.out.println(view7.toString());

    Viewer view8 = new Viewer("Cameron", "BR");

    System.out.println(view8.toString());

    Viewer view9 = new Viewer("Christina", "CN");

    System.out.println(view9.toString());

    Viewer view10 = new Viewer("James", "CR");

    System.out.println(view10.toString());

    Viewer view11 = new Viewer("CarMichael", "DR");

    System.out.println(view11.toString());

    Viewer view12 = new Viewer("Haley", "DN");

    System.out.println(view12.toString());

    Viewer view13 = new Viewer("Lewis", "FR");

    System.out.println(view13.toString());
    }



    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,616
    Thanks
    45
    Thanked 1,956 Times in 1,930 Posts

    Default Re: Program returning wrong values.

    Please Edit your post and wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting


    Also post the output from when you execute the program that shows the problem.
    On windows:
    To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Apr 2012
    Posts
    152
    Thanks
    0
    Thanked 22 Times in 22 Posts

    Default Re: Program returning wrong values.

    Noticed one bit of code which you probably don't want to do
    public Viewer( String Customer, String moviecode)
    {
     
    this.Customer = this.Customer;
     
    this.moviecode = moviecode;
     
    this.cost = cost;
     
    computeCost();
    }

    That is assigning your Customer variable to itself as well as the cost variable. Also, your computeCost() method will always return 9999.99 unless the movie code is N or R.
    Last edited by Parranoia; June 11th, 2012 at 07:59 PM.

  4. #4
    Junior Member
    Join Date
    Jun 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Program returning wrong values.

    Thanks for the help. I got the code to work. My last issue is how to fix the decimal places in my answer. I need to have the numbers with two decimal places ( i.e. 4.53). How do I format my double to only two places?
    My code is:

    public String toString()
    {

    return (Customer + " " + moviecode + " " + "$" + cost) ;

    }
    My output looks like:

    John Smith A $5.925000000000001
    Chris B $5.372000000000001
    Darius C $4.977
    Cheryl D $4.187
    Christine AN $8.425
    Mark AR $6.925000000000001
    Michelle BN $7.872000000000001
    Cameron BR $6.372000000000001
    Christina CN $7.477
    James CR $5.977
    CarMichael DR $5.187
    Haley DN $6.687
    Lewis FR $4.95
    Invalid code Howie J5 $9999.99

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,616
    Thanks
    45
    Thanked 1,956 Times in 1,930 Posts

    Default Re: Program returning wrong values.

    How do I format my double
    Look at the DecimalFormat class.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Member
    Join Date
    Apr 2012
    Posts
    152
    Thanks
    0
    Thanked 22 Times in 22 Posts

    Default Re: Program returning wrong values.

    Or simply using the printf() method
    System.out.printf("%s %s $%.02d", Customer, movieCode, cost);

Similar Threads

  1. What's wrong with simple Scanner program?
    By SV25 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 21st, 2011, 07:05 AM
  2. getTimeInMillis() from the Calendar class returns wrong values
    By 16mydream in forum Java Theory & Questions
    Replies: 2
    Last Post: February 16th, 2011, 12:29 PM
  3. Program not returning expected value.
    By adidez in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 2nd, 2011, 04:16 AM
  4. whats wrong with my program??
    By jrodriguo in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 11th, 2010, 06:16 AM
  5. [SOLVED] Java Beginner: Help with methods and returning values (hailstone program)
    By alf in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 14th, 2010, 06:28 PM