Problem solving and Projectile Motion, ch.7 Flashcards

1
Q

%c

A

Data Type:
char
Prints a single Unicode character

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

%d

A

int, long, short

Prints a decimal integer value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

%o

A

int, long, short

Prints an octal integer value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

%h

A

int, char, long, short

Prints a hexadecimal integer value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

%f

A

float, double

Prints a floating-point value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

%e

A

float, double

Prints a floating-point value in scientific notation

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

%s

A

String

Prints the characters in a String variable or literal

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

%%

A

Prints the “%” character.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

%n

A

Prints the platform-specific new-line character.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Floating-point formatting.
Method calls to printf() apply to PrintStream objects like System.out.

Sub-specifier: width

A

printf(“Value: %7.2f”, myFloat);
Value: 12.34

Specifies the minimum number of characters to print. If the formatted value has more characters than the width, the value will not be truncated. If the formatted value has fewer characters than the width, the output will be padded with spaces (or 0’s if the ‘0’ flag is specified).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Floating-point formatting.
Method calls to printf() apply to PrintStream objects like System.out.

Sub-specifier: .precision

A

printf(“%.4f”, myFloat);
12.3400

printf(“%3.4e”, myFloat);
1.2340e+01

Specifies the number of digits to print following the decimal point. If the precision is not specified, a default precision of 6 is used.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Floating-point formatting.
Method calls to printf() apply to PrintStream objects like System.out.

Sub-specifier: flags

A

printf(“%+f”, myFloat);
+12.340000
printf(“%08.2f”, myFloat);
00012.34

-: Left aligns the output given the specified width, padding the output with spaces.
+: Prints a preceding + sign for positive values. Negative numbers are always printed with the - sign.
0: Pads the output with 0’s when the formatted value has fewer characters than the width.
space: Prints a preceding space for positive value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

float myFloat = 45.1342f;

System.out.printf(“%.3e”, myFloat);

A

4.513e+01

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

float myFloat = 45.1342f;

System.out.printf(“%+.5f”, myFloat);

A

+45.13420

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

float myFloat = 45.1342f;

System.out.printf(“%09.2f”, myFloat);

A

000045.13

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

int myInt = -713;

printf(“%+04d”, myInt);

A

-713

myInt is a negative value, so the + sign is not printed. A - sign is printed for all negative values.

17
Q

int myInt = -713;

printf(“%05d”, myInt);

A

-0713

The 5 indicates 5 characters printed: one character for the - sign, one character for the 0 used to pad the printed value, and 3 characters for the value 713.

18
Q

int myInt = -713;

printf(“%+02d”, myInt);

A

-713

myInt is a negative value, so a - sign is printed for all negative values. The value -713 is 4 characters, more characters than the specified width of 2, so the value is not truncated.

19
Q

String myString = “Testing”;

printf(“%4s”, myString);

A

“Testing”

Strings longer than the specified width are printed without truncation.

20
Q

String myString = “Testing”;

printf(“%8s”, myString);

A

” Testing”

Strings shorter than the specified width are printed with spaces. “Testing” requires 7 characters, so 1 space is added as padding.

21
Q

String myString = “Testing”;

printf(“%.4s”, myString);

A

“Test”

“Testing” is longer than the specified precision and is truncated to 4 characters.

22
Q

String myString = “Testing”;

printf(“%.10s”, myString);

A

“Testing”

A maximum of 10 characters can be printed, but “Testing” has only 7 characters.

23
Q

String myString = “Testing”;

printf(“%-8s123”, myString);

A

“Testing 123”

“Testing” has 7 characters, so the 7 characters of “Testing” are output followed by 1 space as padding. The - flag outputs the padding after the string.

24
Q

How to write for an int

A

System.out.printf(“%1d\n”, myInt);

25
Q

How to write for a double

A

System.out.printf(“%.2f\n”, myDouble1);

26
Q

How to write for a string

A

System.out.printf(“%2s\n”, myString);

System.out.printf(“%-7s|\n”, name1);