In: Computer Science
In Java inputs and output values can easily be manipulated, for example when dealing with money the value should always be formatted to 2 decimals or another use is separating of words in even spaces, line breaks and etc. Your task is to prompt the user for 2 numbers one integer the other decimal and print their product in money format. The program should also take in two words delimitated by @ symbol.
Sample run 1:
Enter a whole number: 4
Enter a decimal number: 6.854
Enter two words delimitated by @ symbol: Mango@15@
Output:
The product of the 2 numbers: 27.416
The product in money format is: N$ 27.42
Assuming the user bought 4 Mango(s) costing N$ 6.85
The VAT to be charged is 15%, hence total due to be paid is N$
31.53
[Hint: The two word provided are the item sold and the VAT charged,
which is added on the product calculation. For formatted printing
and keyboard input see page 129 – 139 in the prescribed book]
In the required code, two numbers has to be taken from the user to calculate the total cost. Also VAT has to be considered, which has to be taken from the user in a string along with the item name, which has to be separated using '@' symbol. Thus, the string can be splitted with respect to '@' symbol in a string array. Where the first value of the array is the item name and the second value will the VAT value in form of string. This can be converted into integer type data using parseInt() function.
The required JAVA program is given below:
import
java.util.*;
//import basic packages
import java.io.*;
public class
Main
//defining a class
{
public static void main(String[]
args) //defining the main
method
{
int wn,
vat;
//declaring required variables
double dn, product,
newp;
String st, v;
char
per='%';
//defining % using character type data to use later
Scanner sc=new
Scanner(System.in);
System.out.printf("Enter
a whole number: ");
wn=sc.nextInt();
//user input of the whole number
System.out.printf("Enter
a decimal number: ");
dn=sc.nextDouble();
//user input of the decimal number
System.out.printf("Enter
two words delimitated by @ symbol: ");
st=sc.nextLine();
//dummy input line since other inputs are taken previously
st=sc.nextLine();
//user input of the item and vat using String
String
words[]=st.split("@");
//splitting the string with respect to '@'' symbol
v=words[1];
//location of the VAT value
vat=Integer.parseInt(v);
//converting the value to integer
product=wn*dn;
//calculaing the total cost excluding VAT
newp=product+product*vat/100;
//calculaing the total cost including VAT
System.out.println("\nThe product
of the 2 numbers:
"+product);
//printing the cost (product) excluding VAT
System.out.printf("The product in
money format is: N$ %.2f",product);
//printing the cost(product) excluding VAT upto 2 decimal
//printing other details
System.out.printf("\nAssuming the
user bought %d %s(s) costing N$ %.2f", wn, words[0], dn);
System.out.printf("\nThe VAT to be
charged is %d %s , hence total due to be paid is N$ %.2f",vat, per,
newp);
}
}
Screenshot of the code:
Output: