Question

In: Computer Science

In Java inputs and output values can easily be manipulated, for example when dealing with money...

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]

Solutions

Expert Solution

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:


Related Solutions

“Strictly speaking, statistics don’t lie, but they can be easily manipulated and distorted.” Then give an...
“Strictly speaking, statistics don’t lie, but they can be easily manipulated and distorted.” Then give an example of where you have seen statistics being used in a non-representative way, in an incorrect way, or taken from an unreliable source. Give measures you will take to ensure that you provide statistics accurately, and in the context they were intended.
Java programming language Array - Single identifier which can store multiple values Example initialized with values:...
Java programming language Array - Single identifier which can store multiple values Example initialized with values: int[] mySimpleArray = {4, 17, 99}; Example with fixed length but no values: int[] myFixedArray = new int[11]; The brackets identify the index. Each value has its own index number. NOTE: First element is the zeroth element: mySimpleArray[0] is 4, [1] is 17 Make a new Netbeans project called ArraysAndLoops Create the simple array with 4, 17, 99. Use System.out.println with the variable with...
Consider a country whose output can be produced with 2 inputs (capital and labor). The output...
Consider a country whose output can be produced with 2 inputs (capital and labor). The output per worker/capita production function is given by y=k1/2, where y represents output per worker/capita and k is capital per worker/capita.  Assume the fraction of output saved/invested is (the savings rate) s = 25%, the population growth rate is 0%, the depreciation rate δ=5%, the level of technology is constant at A=1 and the assumptions of the Solow model hold. What are the steady state levels...
When dealing with issues such as professional ethics, the stakes can be high. This is why...
When dealing with issues such as professional ethics, the stakes can be high. This is why such care is taken to painstakingly clarify terms such as integrity and independence in the AICPA Professional Code of Conduct, as they could otherwise be open to interpretation. In this week’s discussion, you will find illustrative examples of these key principles to share and discuss with your peers. First, review the terms and definitions identified in the “Principles of Professional Conduct” section of the...
When dealing with issues such as professional ethics, the stakes can be high. This is why...
When dealing with issues such as professional ethics, the stakes can be high. This is why such care is taken to painstakingly clarify terms such as integrity and independence in the AICPA Professional Code of Conduct, as they could otherwise be open to interpretation. In this week’s discussion, you will find illustrative examples of these key principles to share and discuss with your peers. First, review the terms and definitions identified in the “Principles of Professional Conduct” section of the...
What are some ethical issues that can arise when dealing with data?
What are some ethical issues that can arise when dealing with data?
When dealing with issues such as professional ethics, the stakes can be high. This is why...
When dealing with issues such as professional ethics, the stakes can be high. This is why such care is taken to painstakingly clarify terms such as integrity and independence in the AICPA Professional Code of Conduct, as they could otherwise be open to interpretation. In this week's discussion, you will find illustrative examples of these key principles to share and discuss with your peers. First, review the terms and definitions identified in the "Principles of Professional Conduct" section of the...
When dealing with issues such as professional ethics, the stakes can be high. This is why...
When dealing with issues such as professional ethics, the stakes can be high. This is why such care is taken to painstakingly clarify terms such as integrity and independence in the AICPA Professional Code of Conduct, as they could otherwise be open to interpretation. In this week’s discussion, you will find illustrative examples of these key principles to share and discuss with your peers. First, review the terms and definitions identified in the “Principles of Professional Conduct” section of the...
Explain the importance of awareness when dealing with employees security. Provide an example of two security...
Explain the importance of awareness when dealing with employees security. Provide an example of two security policies that would help prevent internal employee fraud.
JAVA: This is my code, but when it runs, for the "Average Score" output, it only...
JAVA: This is my code, but when it runs, for the "Average Score" output, it only gives me NaN. How can I fix that? import java.util.Scanner; public class prog4 { public static void main(String[] args) { Scanner reader = new Scanner(System.in); String name; double score; double minScore = 0; double maxScore = 0; int numberOfRecords = 0; double sum = 0; double average = sum / numberOfRecords; System.out.printf("%-15s %-15s %-15s\n", "Student#", "Name", "Score");           while (reader.hasNext()) { name...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT