Paint costs java, clueless | Sololearn: Learn to code for FREE!

+1

Paint costs java, clueless

What is wrong with this code? Got only the first 2 import java.util.Scanner ; class Program { public static void main(String[] args) { Scanner num=new Scanner(System.in); int colour = num.nextInt(); double price =((40+(colour*5))*110/100); Math.round(price); int price2 = (int) price; System.out.print(price2); } }

7/19/2020 2:32:17 PM

Tom Dierckx

11 Answers

New Answer

0

That code compiles fine for me. It also prints the integer values I expected when I input integers 0, 1, ... 3. I don't see a really obvious bug. There are some parts of the code that look odd, though: - The line: "Math.round(price);" does nothing. It calculates the rounded number but stores it nowhere so the remaining code will behave exactly the same way without it. - The expression "((40+(colour*5))*110/100)" always evaluates to an integer so storing the result in a double is pointless, inefficient and makes the code less understandable. - It looks like printing a number with 2 decimal points would be closer to what you really want. Your /100 looks like you want to work with a currency that has 2 decimal places like used in Canada, US, UK, and many other countries. - You should avoid float, double, and any other numeric data type that internally represents the number with a floating decimal point. It is a better practice to store quantities of money in integer data types where 1 would correspond with a cent. Below is what I would update your program to look like. This more clearly names the class, its variables, and tells the user what to type. import java.util.Scanner; public class PaintPriceCalculator { public static void main(String[] args) { Scanner num=new Scanner(System.in); System.out.println("Enter an integer indicating the number of colours of paint you want."); int colour = num.nextInt(); int priceCents =((40+(colour*5))*110); System.out.printf("%d.%02d", priceCents/100, priceCents % 100); } }

0

Thx for the reply but it says that the outcome has to be a number that represents the cost of your purchase rounded up to the nearest whole number.

0

Tom, the following will round up to the nearest whole dollar still without using float or double. import java.util.Scanner; public class PaintPriceCalculator { public static void main(String[] args) { Scanner num=new Scanner(System.in); System.out.println("Enter an integer indicating the number of colours of paint you want."); int colour = num.nextInt(); int priceCents =((40+(colour*5))*110); int priceRoundedUpDollars = priceCents / 100; if (priceCents % 100 != 0) priceRoundedUpDollars++; System.out.println("The price is $" + priceRoundedUpDollars); } }

0

Well I tried that but still I didn't manage to complete the challenge. I changed some stuff and no I got 4 out of 5 completed but for some reason the fifth will not work import java.util.Scanner ; class Program { public static void main(String[] args) { Scanner num=new Scanner(System.in); int colour = num.nextInt(); double price = ((40+(colour*5))*110); int rprice = (int)Math.round((price/100)); System.out.print(rprice); } }

0

I didn't do any of Sololearn's challenges because they're restricted to mobile phones. I just didn't want to install the app. Do you have a screenshot from it? I wonder if there's some missed requirements.

0

Here you go šŸ˜‰ and thx for the effort already Paint costs +10 XP You are getting ready to paint a piece of art. The canvas and brushes that you want to use will cost 40.00. Each color of paint that you buy is an additional 5.00. Determine how much money you will need based on the number of colors that you want to buy if tax at this store is 10%. Task Given the total number of colors of paint that you need, calculate and output the total cost of your project rounded up to the nearest whole number. Input Format An integer that represents the number of colors that you want to purchase for your project. Output Format A number that represents the cost of your purchase rounded up to the nearest whole number. Sample Input 10 Sample Output 99

0

Maybe convert the "int" to "long". It is a shot in the dark but maybe there's a test case that goes beyond range for 32-bit integers. It is hard to imagine what could be in that other test case. That question matches the math in the program from what I can see. Here are some thoughts but none point out a clear problem. A negative number like -1 colours would be a weird case. The problem description doesn't say to throw an exception or anything specific if -1 is entered. It would be weird if they added a test case for inputs out of the specified range. For input 0, output 44 looks correct to me and that's what the program prints. That is very likely one of the test cases. Inputting a number like 3 billion would be a little weird too. That would be large enough to go out of range for int and introduce lots of error if you used the double data type. If 3 billion was a test case, the implementation could easily fail but you could convert the "int" to "long" to fix it.

0

Tried long but didn't work. Maybe someone can give the code that works so we can see what I did wrong. I am here to learn ...

0

Looks like I found it import java.util.Scanner ; class Program { public static void main(String[] args) { Scanner num=new Scanner(System.in); int colour = num.nextInt(); double price = ((40.00+(colour*5))*110); int rprice = (int)Math.round((price/100)); System.out.print(rprice); } }

0

Changed 40 into 40.00

0

import java.util.Scanner ; public class Program { public static void main(String[] args) { Scanner input=new Scanner(System.in); int colors=input.nextInt(); double cost=colors*5+40.00; double p=cost*10/100; System.out.println((int)Math.round(cost+p)); } }