This site requires JavaScript, please enable it in your browser!
Greenfoot back
dbutts1@stumail.com
dbutts1@stumail.com wrote ...

2017/12/16

Changing an int to a double using two methods

dbutts1@stumail.com dbutts1@stumail.com

2017/12/16

#
The user will enter 30, but to get the math to work, we need to turn this into .3. You can do this by dividing by 100 or multiplying it by .01. Then you multiply by the original price to determine the amount of the discount. This logic, to get the discount amount, should be in a separate method, which we call from the act method. We want to show we can write a method that calculates a value and return, and also that we can call that method properly. So this method will be setup to accept the price of the product, and the discount percentage (the original integer value). The logic in the method will then do the math above, converting it to a real percentage and multiplying the price. It then returns that discount amount.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.text.DecimalFormat;
/**
 * Write a description of class MyWorld here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class MyWorld extends World
{
 
    /**
     * Constructor for objects of class MyWorld.
     *
     */
    public MyWorld()
    {   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1);
    }
 
    public void act()
    {
 
        DecimalFormat money = new DecimalFormat("$0.00");
        double price;
        int discount;
        price=Double.parseDouble(Greenfoot.ask("What is the price of the product?"));
        discount = Integer.parseInt(Greenfoot.ask(" What is the amount of the discount? Please enter as in a numerical value of two numbers, such as 35 for 35% "));
        discountAmount();
        showText("Price: " + money.format(price), getWidth()/2, getHeight()/2);
        showText("Discount: " + money.format(discount) , getWidth()/2, getHeight()/2 + 20);
        showText("Sale Price: " + money.format(disconutAmount()), getWidth()/2, getHeight()/2 + 40);
        Greenfoot.stop();
 
    }
    public double discountAmount()
    {
 
         
        double discountAmount = (price*(discount/100));
        return discountAmount;
 
    }
 
}
dbutts1@stumail.com dbutts1@stumail.com

2017/12/16

#
I am not getting any errors showing when I compile or save , but the actual sub world, myWorld will not compile. I worked on one like this yesterday, but it was all as one method.
Super_Hippo Super_Hippo

2017/12/16

#
Line 33 has a typo: 'disconut' which is actually quite funny. (Line 30 can be removed.)
dbutts1@stumail.com dbutts1@stumail.com

2017/12/17

#
I removed line 30 and fixed the 'disconut,' he is gone no one likes a' disconut' for very long. I moved my variables out so they would not be restricted to being local variables. I am still having the problem with the return statement in public double discountAmount() , I am getting the 'class.' expected error. I have been reading Java tutorials on returning a value, but cannot find my error. I am placing my revised code below.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.text.DecimalFormat;
/**
 * Write a description of class MyWorld here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class MyWorld extends World
{
 
    /**
     * Constructor for objects of class MyWorld.
     *
     */
    public MyWorld()
    {   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1);
         
    }
     
    int discount;
    double price;
    double salePrice;
    public void act()
    {
        DecimalFormat money = new DecimalFormat("$0.00");
         
        price=Double.parseDouble(Greenfoot.ask("What is the price of the product?"));
        discount = Integer.parseInt(Greenfoot.ask(" What is the amount of the discount? Please enter as in a numerical value of two numbers, such as 35 for 35% "));
 
        showText("Price: " + money.format(price), getWidth()/2, getHeight()/2);
        showText("Discount: " + money.format(discount) , getWidth()/2, getHeight()/2 + 20);
        showText("Sale Price: " + money.format(discountAmount), getWidth()/2, getHeight()/2 + 40);
 
    }
    
 
    public double discountAmount()
    {
 
         double salePrice = (price*(discount/100));
         return double salePrice;
 
    }
 
     
}
Super_Hippo Super_Hippo

2017/12/17

#
You have to remove the double in line 44. However, in the last code, you didn't have this extra double. You changed the disconut, but removed the () after it which is needed to call the method. You can change the method to the following but besides the extra double, it doesn't change anything:
1
2
3
4
private double discountAmount()
{
    return price*discount/100;
}
dbutts1@stumail.com dbutts1@stumail.com

2017/12/17

#
Thank You
You need to login to post a reply.