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...
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...
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.
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 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
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...
Write a Console Java program that inserts 25 random integers in the range of 0 to...
Write a Console Java program that inserts 25 random integers in the range of 0 to 100 into a Linked List. (Use SecureRandom class from java.security package. SecureRandom rand = new SecureRandom(); - creates the random number object rand.nextInt(100) - generates random integers in the 0 to 100 range) Using a ListItreator output the contents of the LinkedList in the reverse order. Using a ListItreator output the contents of the LinkedList in the original order.
Write a Console Java program that inserts 25 random integers in the range of 0 to...
Write a Console Java program that inserts 25 random integers in the range of 0 to 100 into a Linked List. (Use SecureRandom class from java.security package. SecureRandom rand = new SecureRandom(); - creates the random number object rand.nextInt(100) - generates random integers in the 0 to 100 range) Using a ListItreator output the contents of the LinkedList in the original order. Using a ListItreator output the contents of the LinkedList in the reverse order.
write a java code to represent a sales class as follows: 1- The Sales class contains...
write a java code to represent a sales class as follows: 1- The Sales class contains the names of sellers (strings) and the sales/seller/day (matrix of integers). Assume the number of sellers is set dynamically by the constructor and that the sellers work 6 days/week. Example: names/days 0 1 2 3 4 5 Ali 30 5 89 71 90 9 Ahmad 15 81 51 69 78 25 Omar 85 96 7 87 41 54 The class should contain the following...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT