Question

In: Computer Science

The NumberFormat Class 1.Write a Java statement that shows to the console the monetary value formatting...

The NumberFormat Class

1.Write a Java statement that shows to the console the monetary value formatting of the double variable called 'price'.

2.Write a Java statement that shows to the console the percentage formatting of the double variable 'tax'.

3.If you applied the monetary value formatting to the value 18, the result will be:

4.If you applied the percentage formatting to the double 0.068, the result will be:

The DecimalFormat Class.

1.Write a Java statement that creates a formatting object called 'fmt'. The pattern indicates that at least one digit should be printed to the left of the decimal point and should be zero if the integer portion of the value is zero. It also indicates that the fractional portion of the value should be rounded to four digits.

Remeber about the Softchalk limitiation on using double quotation mark("). Please for this exercise use just single quotations (') instead.

2.Write a Java statement that creates a formatting object called 'fmt1'. The pattern indicates that at least one digit should be printed to the left of the decimal point and should not shown if the integer portion of the value is zero. It also indicates that the fractional portion of the value should be rounded to three digits.

Remeber about the Softchalk limitiation on using double quotation mark("). Please for this exercise use just single quotations (') instead.

3.Write a statement that show to the console the application of the fmt formatting to the variable 'value'.

4.What is the result if you apply the fmt1 formatting to the value 0.4582?

5.If we apply the following pattern "#.####" to the value 12.42998122, what is the formatted value shown to the console?

6.If we apply the following pattern "0.000" to the value 24.3 what is the formatted value shown to the console?

The string class

1.

Given the following declaration:

char oneChar;

Write a statement that stores the third character of the string object called "city" in oneChar.

2.

Given the following delcaration:

String modification2;

Write a statement that replaces all the characters 'e' by # in the string object 'city', and stores the modified string in the modification2..

3.

Given the following declaration:

int stringSize;

Write a statement that stores the length of the string object called "city" in stringSize.

4.

Given the following delcaration:

String modification1;

Write a statement that stores the uppercase equivalent of the string object called "city" in modification1.

5.

Given the following delcaration:

String book = "The Lord of the Rings";

What is the output proced by the following statement?

System.out.println(book.substring(4,10));

6.

Write a statement that declares a String variable named city. The variable should be initialized with the string literal "Los Angeles".

Note: we now that string literals are enclosed in double quotation marks, but there is a limitation in Softchalk and we cannot use double quotation mark in your answers. Therefore, just for this exericise, plase use a sigle (') quotation instead of (").

7

Given the following delcaration:

String modification3;

Write a statement that stores the lowercase equivalent of the string object 'city' in modification3.

Solutions

Expert Solution

Note : I will update the String class demo also..thank u

/********* NumberFormatDemo.java ********/

import java.text.NumberFormat;

public class NumberFormatDemo {

   public static void main(String[] args) {
  
       double price=18,tax=0.068;
       NumberFormat fmt;
       fmt= NumberFormat.getCurrencyInstance();

// 1
       System.out.println("Price :"+fmt.format(price));
       fmt = NumberFormat.getPercentInstance();

// 2
       System.out.println("Percentage :"+fmt.format(tax));
      

   }

}

/***************************************************/

/***************************************************/

Output:

/***************************************************/

/********* DecimalFormatDemo.java ***********/

import java.text.DecimalFormat;

public class DecimalFormatDemo {

   public static void main(String[] args) {

       double val1=0.4582;
       //DecimalFormat class is used to format the output
               DecimalFormat fmt,fmt1;
       fmt = new DecimalFormat("#.####");
       fmt1= new DecimalFormat(".000");

// 1
       System.out.println("Value :"+fmt1.format(val1));
       val1=12.42998122;
// 2
       System.out.println("Value :"+fmt.format(val1));
       val1=24.3;
       fmt1= new DecimalFormat("0.000");

// 3
       System.out.println("Value :"+fmt1.format(val1));
      
   }

}

/***************************************************/

/***************************************************/

Output:

/***************************************************/


Related Solutions

DO THIS PROGRAM IN JAVA Write a complete Java console based program following these steps: 1....
DO THIS PROGRAM IN JAVA Write a complete Java console based program following these steps: 1. Write an abstract Java class called Shape which has only one abstract method named getArea(); 2. Write a Java class called Rectangle which extends Shape and has two data membersnamed width and height.The Rectangle should have all get/set methods, the toString method, and implement the abstract method getArea()it gets from class Shape. 3. Write the driver code tat tests the classes and methods you...
JAVA Write a Java console application that prompts the user to enter the radius of a...
JAVA Write a Java console application that prompts the user to enter the radius of a circle, then prints its radius, diameter, circumference, and area. The Console Output Enter the radius of the circle: 1.2 The radius is 1.2 The diameter is 2.4 The circumference is 7.5398223686155035 The area is 4.523893421169302 Programmer Notes Write and document your program per class coding conventions. Add an instance variable double radius. Generate its get/set methods. Manually type the methods getDiameter(), getCircumference(), and getArea()....
Write the code in Java: 1. Create a method that displays your name in the console....
Write the code in Java: 1. Create a method that displays your name in the console. This method is void and takes no parameters. Make an app that runs the method in response to a button press. 2. Create a version of the method in #1 that takes the text (String) to be displayed as a parameter. Allow the user to enter the text in a dialog box or text field and display that text in the console. Be sure...
Language is Java Design and write a Java console program to estimate the number of syllables...
Language is Java Design and write a Java console program to estimate the number of syllables in an English word. Assume that the number of syllables is determined by vowels as follows. Each sequence of adjacent vowels (a, e, i, o, u, or y), except for a terminal e, is a syllable. However, the minimum number of syllables in an English word is one. The program should prompt for a word and respond with the estimated number of syllables in...
In VB console write a Console Application that reads an input value for ??. You can...
In VB console write a Console Application that reads an input value for ??. You can assume that the user only inputs numeric values.
Write a statement to output on the console the valueof the last element in a...
Write a statement to output on the console the value of the last element in a single-dimensional array of integers called grades.
Write a Java console application that reads a string for a date in the U.S. format...
Write a Java console application that reads a string for a date in the U.S. format MM/DD/YYYY and displays it in the European format DD.MM.YYYY  For example, if the input is 02/08/2017, your program should output 08.02.2017
URGENT!!! DO THIS PROGRAM IN JAVA Write a complete Java console based program following these steps:...
URGENT!!! DO THIS PROGRAM IN JAVA Write a complete Java console based program following these steps: 1. Write an abstract Java class called Shape which has only one abstract method named getArea(); 2. Write a Java class called Rectangle which extends Shape and has two data membersnamed width and height.The Rectangle should have all get/set methods, the toString method, and implement the abstract method getArea()it gets from class Shape. 3. Write the driver code tat tests the classes and methods...
Write a Java code to represent a 1. Date class. As date class is composed of...
Write a Java code to represent a 1. Date class. As date class is composed of three attributes, namely month, year and day; so the class contains three Data Members, and one method called displayDate() which will print these data members. Test the Date class using main class named DateDemo. Create two objects of date class. Initialize the data fields in Date class using the objects, invoke the method displyaDate(). Date month : String year: int day : int displayDate():...
Write 2 short Java programs based on the description below. 1) Write a public Java class...
Write 2 short Java programs based on the description below. 1) Write a public Java class called WriteToFile that opens a file called words.dat which is empty. Your program should read a String array called words and write each word onto a new line in the file. Your method should include an appropriate throws clause and should be defined within a class called TextFileEditor. The string should contain the following words: {“the”, “quick”, “brown”, “fox”} 2) Write a public Java...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT