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());
}
}
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.
Re: Program returning wrong values.
Noticed one bit of code which you probably don't want to do
Code java:
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.
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
Re: Program returning wrong values.
Quote:
How do I format my double
Look at the DecimalFormat class.
Re: Program returning wrong values.
Or simply using the printf() method
Code java:
System.out.printf("%s %s $%.02d", Customer, movieCode, cost);