Lab 1: Using Bluej at Uofw
Total Page:16
File Type:pdf, Size:1020Kb
ACS-1903 Lab 3: printf, DecimalFormat, if Fall 2007
printf, DecimalFormatter, & if
Lab 3 is to be done outside of class and lab 3D03. You may use lab 3C13 or your own computing resources to support your work.
When you are showing the output from a program or code segment you must be precise with repect to the line produced and the number of character positions used up.
Place your answers on the lab paper itself and hand these in to your instructor (3D15) or to the ACS Office (3D11) on or before 4:00pm Monday Oct 1. There are 6 questions.
Student Identification
Student number ______
Last name ______
First name ______
Workstation user id ______
Email address ______
1. (See pages 146-148.) Change each of the following to single-line Java statements that use a conditional expression.
if (a>100) c=100; else c=a; ______
if (a==100) c=0; else c+=1; ______
if (a==28) a=1; else a*=2; ______
if (b<0) a=0; else if (b>100)a=100; else a=b;
______
1 ACS-1903 Lab 3: printf, DecimalFormat, if Fall 2007
2. (See pages 126-130.) Complete the following to find the largest of 3 integers read from Standard Input. Note that you must use a nested if statement.
import java.util.Scanner; public class FindMaximum { public static void main(String[] args) { Scanner keyboard = new Scanner (System.in); int number1, number2, number3, numberMax; System.out.println("enter 3 numbers: "); number1 = keyboard.nextInt(); number2 = keyboard.nextInt(); number3 = keyboard.nextInt();
System.out.print("the largest of the 3 numbers " +number1+" "+number2+" and "+number3+" is: "+numberMax); System.out.println(); } }
3. (See pages 161-164.) What is the output from
String s = "the string <%25s>"; System.out.printf(s,s);
______
4. (See pages 161-164.) What is the output from the following
int num=6; int r = num % 2; num = num / 2; int s = num % 2; num = num / 2; int t = num % 2; num = num / 2; System.out.printf("%1d%1d%1d", t,s,r);
______
2 ACS-1903 Lab 3: printf, DecimalFormat, if Fall 2007
5. (See pages 161-164.) What is the output from the following
int num=623; int r = num % 10; num = num / 10; int s = num % 10; num = num / 10; int t = num % 10; num = num / 10; System.out.printf("%1d%1d%1d", t,s,r);
______
6. (See pages 155-160.) Consider the following program.
import java.text.DecimalFormat;
public class lab03Format { public static void main(String[] args) { double number1 = 12.3; double number2 = 45.6; double number3 = 78.9; String string1, string3;
DecimalFormat formatter1 = new DecimalFormat("##0"); DecimalFormat formatter2 = new DecimalFormat("000"); DecimalFormat formatter3 = new DecimalFormat("000.00");
string1 = formatter1.format(number1); System.out.println(string1); System.out.println(formatter2.format(number2)); string3 = formatter3.format(number3); System.out.println(string3); } } What is the output from the above?
How many objects are created and what are their identifiers (names) in the above code?
What message (method name) is sent to the DecimalFormat objects in the above code?
3