Results 1 to 13 of 13
  1. #1
    Join Date
    Jul 2008
    Posts
    6
    Rep Power
    0

    Default Help with muliplying two variables

    I am just introducing myself to java, and in the book there is a question which i am stuck with any help ?? the question:

    "write an assignment statement that will store the result of multiplying two variables, price and discount, into a third variable saving."


    My code is below but i cannot get the code to store the result in the saving variable is there any one who could help ammending this so i could understand ??

    /**
    * TicketMachine models a ticket machine that issues
    * flat-fare tickets.
    * The price of a ticket is specified via the constructor.
    * Instances will check to ensure that a user only enters
    * sensible amounts of money, and will only print a ticket
    * if enough money has been input.
    *
    * @author David J. Barnes and Michael Kolling
    * @version 2006.03.30
    */
    public class TicketMachine
    {
    // The price of a ticket from this machine.
    private int price;
    // The amount of money entered by a customer so far.
    private int balance;
    // The total amount of money collected by this machine.
    private int total;
    // The amount of money discounted by on the ticket.
    private int discount;
    // Saving on the ticket.
    private int saving;

    /**
    * Create a machine that issues tickets of the given price.
    */
    public TicketMachine(int ticketCost)
    {
    price = ticketCost;
    balance = 0;
    total = 0;
    saving = 0;
    }

    /**
    * @Return The price of a ticket.
    */
    public int getPrice()
    {
    return price;
    }

    /**
    * Return The amount of money already inserted for the
    * next ticket.
    */
    public int getBalance()
    {
    return balance;
    }

    /**
    * Discounting amount off of the price of the ticket.
    */
    public void discount(int amount)
    {
    discount -= amount;
    price += discount;
    }

    public void saving(int saving)
    {
    int price = 0;
    int discount = 0;
    int result = price * discount;
    }

    /**
    * Receive an amount of money in cents from a customer.
    * Check that the amount is sensible.
    */
    public void insertMoney(int amount)
    {
    if(amount > 0) {
    balance = balance + amount;
    }
    else {
    System.out.println("Use a positive amount: " +
    amount);
    }
    }

    /**
    * Print a ticket if enough money has been inserted, and
    * reduce the current balance by the ticket price. Print
    * an error message if more money is required.
    */
    public void printTicket()
    {
    if(balance >= price) {
    // Simulate the printing of a ticket.
    System.out.println("##################");
    System.out.println("# The BlueJ Line");
    System.out.println("# Ticket");
    System.out.println("# " + price + " cents.");
    System.out.println("##################");
    System.out.println();

    // Update the total collected with the price.
    total = total + price;
    // Reduce the balance by the prince.
    balance = balance - price;
    }
    else {
    System.out.println("You must insert at least: " +
    (price - balance) + " more cents.");

    }
    }

    /**
    * Return the money in the balance.
    * The balance is cleared.
    */
    public int refundBalance()
    {
    int amountToRefund;
    amountToRefund = balance;
    balance = 0;
    return amountToRefund;
    }
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    i cannot get the code to store the result in the saving variable
    If you'd highlight the problem area with a string that we could use Find on, it would be easier to find it. For example: add a comment
    //**HERE and then tell us to look for //**HERE.

    If you are getting any error messages, copy and paste them here.
    Last edited by Norm; 07-16-2008 at 11:14 PM.

  3. #3
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    7

    Default

    Welcome michael.dunkling, to the Forums! ;)

    Look at this code here. Maybe it will help.
    Java Code:
    /**
    * TicketMachine models a ticket machine that issues
    * flat-fare tickets.
    * The price of a ticket is specified via the constructor.
    * Instances will check to ensure that a user only enters
    * sensible amounts of money, and will only print a ticket
    * if enough money has been input.
    *
    * @author David J. Barnes and Michael Kolling
    * @version 2006.03.30
    */
    public class TicketMachine
    {
    	// The price of a ticket from this machine.
    	private int price;
    	// The amount of money entered by a customer so far.
    	private int balance;
    	// The total amount of money collected by this machine.
    	private int total;
    	// The amount of money discounted by on the ticket.
    	private int discount;
    	// Saving on the ticket.
    	private int saving;
    
    	/**
    	* Create a machine that issues tickets of the given price.
    	*/
    	public TicketMachine(int ticketCost) {
    		price = ticketCost;
    		balance = 0;
    		total = 0;
    		saving = 0;
    	}
    
    	/**
    	* @Return The price of a ticket.
    	*/
    	public int getPrice(){
    		return price;
    	}
    
    	/**
    	* Return The amount of money already inserted for the
    	* next ticket.
    	*/
    	public int getBalance(){
    		return balance;
    	}
    
    	/**
    	* Discounting amount off of the price of the ticket.
    	*/
    	public void discount(int amount){
    		discount [COLOR="RoyalBlue"]+[/COLOR]= amount;
    		price [COLOR="RoyalBlue"]-[/COLOR]= discount;
    	}
    
    	[COLOR="Red"]/*[/COLOR]
    	public void saving(int [COLOR="RoyalBlue"]saving[/COLOR]){
    		int price = 0;
    		int discount = 0;
    		int result = price * discount;
    		[COLOR="RoyalBlue"]// result will always be zero. 
    		// saving was never used.[/COLOR]
    	}
    	[COLOR="Red"]*/[/COLOR]
    [COLOR="RoyalBlue"]	// Maybe this will help:
    	public void save(){
    		saving = price * discount;
    	}[/COLOR]
    
    	/**
    	* Receive an amount of money in cents from a customer.
    	* Check that the amount is sensible.
    	*/
    	public void insertMoney(int amount) {
    		if(amount > 0) {
    			balance = balance + amount;
    		} else {
    			System.out.println("Use a positive amount: " + amount);
    		}
    	}
    
    	/**
    	* Print a ticket if enough money has been inserted, and
    	* reduce the current balance by the ticket price. Print
    	* an error message if more money is required.
    	*/
    	public void printTicket(){
    		if(balance >= price) {
    			// Simulate the printing of a ticket.
    			System.out.println("##################");
    			System.out.println("# The BlueJ Line");
    			System.out.println("# Ticket");
    			System.out.println("# " + price + " cents.");
    			System.out.println("##################");
    			System.out.println();
    
    			// Update the total collected with the price.
    			total = total + price;
    			// Reduce the balance by the prince.
    			balance = balance - price;
    		} else {
    			System.out.println("You must insert at least: " + (price - balance) + " more cents.");
    		}
    	}
    
    	/**
    	* Return the money in the balance.
    	* The balance is cleared.
    	*/
    	public int refundBalance() {
    		int amountToRefund;
    		amountToRefund = balance;
    		balance = 0;
    		return amountToRefund;
    	}
    }
    Also, try using indentation to make your code look more readable and professional. You can use color tags to highlight areas with uncertainty. Look at the tool bar next to the smiley. The important peaces of code is highlighted in blue. Please use code tags in the future.

    I hope you enjoy your stay at Java Forums and good luck. :p
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

  4. #4
    Join Date
    Jul 2008
    Posts
    6
    Rep Power
    0

    Default Help with multiplying two variables and storing result in a 3rd variable

    Unfortunately this code still does not store the result into the savings variable it still is displaying 0. I think this is one of the ways i tried yesterday but the Bluej system does not seem to want to display it in the correct field. Shame i was working for the Objects first with java book and this is the only problem i have come across which i cannot solve myself.

    Thankyou for your response if there are any other ideas which may help me then i would be gratefull trying to prepare myself for university by learning java on my own lol do you think this is wise???

    /**
    * TicketMachine models a ticket machine that issues
    * flat-fare tickets.
    * The price of a ticket is specified via the constructor.
    * Instances will check to ensure that a user only enters
    * sensible amounts of money, and will only print a ticket
    * if enough money has been input.
    *
    * @author David J. Barnes and Michael Kolling
    * @version 2006.03.30
    */
    public class TicketMachine
    {
    // The price of a ticket from this machine.
    private int price;
    // The amount of money entered by a customer so far.
    private int balance;
    // The total amount of money collected by this machine.
    private int total;
    // The amount of money discounted by on the ticket.
    private int discount;
    // Saving on the ticket.
    private int saving;

    /**
    * Create a machine that issues tickets of the given price.
    */
    public TicketMachine(int ticketCost) {
    price = ticketCost;
    balance = 0;
    total = 0;
    saving = 0;
    }

    /**
    * @Return The price of a ticket.
    */
    public int getPrice(){
    return price;
    }

    /**
    * Return The amount of money already inserted for the
    * next ticket.
    */
    public int getBalance(){
    return balance;
    }

    /**
    * Discounting amount off of the price of the ticket.
    */
    public void discount(int amount){
    discount += amount;
    price -= discount;
    }

    /**
    * Maybe this will help:
    */

    public void save(){
    saving = price * discount;
    }

    /**
    * Receive an amount of money in cents from a customer.
    * Check that the amount is sensible.
    */
    public void insertMoney(int amount) {
    if(amount > 0) {
    balance = balance + amount;
    } else {
    System.out.println("Use a positive amount: " + amount);
    }
    }

    /**
    * Print a ticket if enough money has been inserted, and
    * reduce the current balance by the ticket price. Print
    * an error message if more money is required.
    */
    public void printTicket(){
    if(balance >= price) {
    // Simulate the printing of a ticket.
    System.out.println("##################");
    System.out.println("# The BlueJ Line");
    System.out.println("# Ticket");
    System.out.println("# " + price + " cents.");
    System.out.println("##################");
    System.out.println();

    // Update the total collected with the price.
    total = total + price;
    // Reduce the balance by the prince.
    balance = balance - price;
    } else {
    System.out.println("You must insert at least: " + (price - balance) + " more cents.");
    }
    }

    /**
    * Return the money in the balance.
    * The balance is cleared.
    */
    public int refundBalance() {
    int amountToRefund;
    amountToRefund = balance;
    balance = 0;
    return amountToRefund;
    }
    }

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    it still is displaying 0
    Where is the code that doesn't work?

  6. #6
    Join Date
    Jul 2008
    Posts
    6
    Rep Power
    0

    Default Problem in red

    The area i am having prolems with is highlighted in red. It will not store the result of the variable multiplication : discount * price. i have tried several different solutions. The value has to be stored in the variable saving, any ideas at all???

    /**
    * TicketMachine models a ticket machine that issues
    * flat-fare tickets.
    * The price of a ticket is specified via the constructor.
    * Instances will check to ensure that a user only enters
    * sensible amounts of money, and will only print a ticket
    * if enough money has been input.
    *
    * @author David J. Barnes and Michael Kolling
    * @version 2006.03.30
    */
    public class TicketMachine
    {
    // The price of a ticket from this machine.
    private int price;
    // The amount of money entered by a customer so far.
    private int balance;
    // The total amount of money collected by this machine.
    private int total;
    // The amount of money discounted by on the ticket.
    private int discount;
    // Saving on the ticket.
    private int saving;

    /**
    * Create a machine that issues tickets of the given price.
    */
    public TicketMachine(int ticketCost) {
    price = ticketCost;
    balance = 0;
    total = 0;
    saving = 0;
    }

    /**
    * @Return The price of a ticket.
    */
    public int getPrice(){
    return price;
    }

    /**
    * Return The amount of money already inserted for the
    * next ticket.
    */
    public int getBalance(){
    return balance;
    }

    /**
    * Discounting amount off of the price of the ticket.
    */
    public void discount(int amount){
    discount += amount;
    price -= discount;
    }


    /**
    * Receive an amount of money in cents from a customer.
    * Check that the amount is sensible.
    */
    public void insertMoney(int amount) {
    if(amount > 0) {
    balance = balance + amount;
    } else {
    System.out.println("Use a positive amount: " + amount);
    }
    }

    /**
    * Print a ticket if enough money has been inserted, and
    * reduce the current balance by the ticket price. Print
    * an error message if more money is required.
    */
    public void printTicket(){
    if(balance >= price) {
    // Simulate the printing of a ticket.
    System.out.println("##################");
    System.out.println("# The BlueJ Line");
    System.out.println("# Ticket");
    System.out.println("# " + price + " cents.");
    System.out.println("##################");
    System.out.println();

    // Update the total collected with the price.
    total = total + price;
    // Reduce the balance by the prince.
    balance = balance - price;
    } else {
    System.out.println("You must insert at least: " + (price - balance) + " more cents.");
    }
    }

    /**
    * Return the money in the balance.
    * The balance is cleared.
    */
    public int refundBalance() {
    int amountToRefund;
    amountToRefund = balance;
    balance = 0;
    return amountToRefund;
    }
    }

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    value has to be stored in the variable saving
    The only way to get a value into a variable is by assigning it:

    saving = <the value>;

    Where do you have an assignment statement that sets saving other than the one in the constructor that sets it to 0?

    Where is the expression: discount * price ?

    I would expect to see: saving = discount * price;
    somewhere???

  8. #8
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    7

    Default

    Quote Originally Posted by michael.dunkling
    Thankyou for your response if there are any other ideas which may help me then i would be gratefull trying to prepare myself for university by learning java on my own lol do you think this is wise???
    Yes, it is wise to study ahead. ;) I did the same with a good Java book and that helped me greatly.
    Quote Originally Posted by michael.dunkling
    The area i am having prolems with is highlighted in red.
    The red code does not include usage of the saving variable, nor multiplication. Are you calling the save() method in your program somewhere?
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

  9. #9
    Join Date
    Jul 2008
    Posts
    6
    Rep Power
    0

    Default

    Now i've looked at my code i have stated that saving = discount * price; in the top part of my code. I do not want to call a method to get the result to store in saving i simply want the code to multiply the values at price and discount and simply put the result into the saving variable. i thought this would have just been a simple thing but the code doesnt seem to work is it possible to try and get the code to do this with some helpful comments so i can understand where i have gone wrong??

    I'm also glad to hear that im doing the right thing many of my friends say that im in a summer holida why am i still studying but i just feel that up to now i have picked java up quite naturally and this hopefully means that if i study i will eventually be a successfull coder. so thanks for the advice

    /**
    * TicketMachine models a ticket machine that issues
    * flat-fare tickets.
    * The price of a ticket is specified via the constructor.
    * Instances will check to ensure that a user only enters
    * sensible amounts of money, and will only print a ticket
    * if enough money has been input.
    *
    * @author David J. Barnes and Michael Kolling
    * @version 2006.03.30
    */
    public class TicketMachine
    {
    // The price of a ticket from this machine.
    private int price;
    // The amount of money entered by a customer so far.
    private int balance;
    // The total amount of money collected by this machine.
    private int total;
    // The amount of money discounted by on the ticket.
    private int discount;
    // Saving on the ticket.
    private int saving;

    /**
    * Create a machine that issues tickets of the given price.
    */
    public TicketMachine(int ticketCost)
    {
    price = ticketCost;
    balance = 0;
    total = 0;
    saving = discount * price;
    }

    /**
    * @Return The price of a ticket.
    */
    public int getPrice()
    {
    return price;
    }

    /**
    * Return The amount of money already inserted for the
    * next ticket.
    */
    public int getBalance()
    {
    return balance;
    }

    /**
    * Discounting amount off of the price of the ticket.
    */
    public void discount(int amount)
    {
    discount -= amount;
    price += discount;
    }

    public void saving(int saving)
    {
    int price = 0;
    int discount = 0;
    saving = discount * price;
    }


    /**
    * Receive an amount of money in cents from a customer.
    * Check that the amount is sensible.
    */
    public void insertMoney(int amount)
    {
    if(amount > 0) {
    balance = balance + amount;
    }
    else {
    System.out.println("Use a positive amount: " +
    amount);
    }
    }

    /**
    * Print a ticket if enough money has been inserted, and
    * reduce the current balance by the ticket price. Print
    * an error message if more money is required.
    */
    public void printTicket()
    {
    if(balance >= price) {
    // Simulate the printing of a ticket.
    System.out.println("##################");
    System.out.println("# The BlueJ Line");
    System.out.println("# Ticket");
    System.out.println("# " + price + " cents.");
    System.out.println("##################");
    System.out.println();

    // Update the total collected with the price.
    total = total + price;
    // Reduce the balance by the prince.
    balance = balance - price;
    }
    else {
    System.out.println("You must insert at least: " +
    (price - balance) + " more cents.");

    }
    }

    /**
    * Return the money in the balance.
    * The balance is cleared.
    */
    public int refundBalance()
    {
    int amountToRefund;
    amountToRefund = balance;
    balance = 0;
    return amountToRefund;
    }
    }

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    the code doesnt seem to work
    Can you copy and post the output here and describe what is wrong with the output and how you want it to change?
    int price = 0;
    int discount = 0;
    saving = discount * price;
    }
    if the values of price an discount are both 0 what will be the value of saving? 0 * 0 = ???

    I think you should stop coding for a minute and play computer with a paper an pencil. Find where the program starts, the main method and write down the values of all the variables on the paper. Then go step by step thru the program and as the program changes the value of a variable, make that change on your paper. Go slowly and don't skip around, one step at a time.

  11. #11
    Join Date
    Jul 2008
    Posts
    6
    Rep Power
    0

    Default

    Here is the result of my code. Here i entered the value of 1000 as the original ticket price. I then discounted 200. Which is what is now displayed the price is noww 800 and discount shows the amount been deducted but as you can also see that the savings variable is still 0. I have been working through like you said all the way up til now, but still cant see how this is possible to get this to be displayed in this field. sorry if im just a pain lol
    Attached Thumbnails Attached Thumbnails Help with muliplying two variables-fd.jpg  

  12. #12
    Join Date
    Jul 2008
    Posts
    6
    Rep Power
    0

    Default

    thanks you guys for all your help but when it came down to it i just needed to believe that i could achieve it and have managed to do exactly what i needed it to do so thankyou you all for your help but i guess this is how you got where you are by believing and trying yourselves thanks again and will no doubt be posting more comments on this forum at some point lol

  13. #13
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    7

    Default

    Quote Originally Posted by michael.dunkling
    sorry if im just a pain lol
    michael.dunkling, you are not a pain. Can you please post the main class of your program, i.e. the one containing the main() method. Then I can see how the methods are used to give your program output. ;)
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

Similar Threads

  1. boolean variables
    By ravian in forum New To Java
    Replies: 3
    Last Post: 12-31-2007, 04:58 AM
  2. Variables
    By mew in forum New To Java
    Replies: 3
    Last Post: 12-11-2007, 12:44 PM
  3. JSP - session variables
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-02-2007, 09:22 PM
  4. variables-methods
    By Warren in forum New To Java
    Replies: 1
    Last Post: 11-28-2007, 04:14 PM
  5. Help with variables in java
    By fernando in forum New To Java
    Replies: 2
    Last Post: 08-06-2007, 05:03 PM

Posting Permissions

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