In: Computer Science
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.
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:
/***************************************************/