- Write a declaration to declare a variable to represent each of the following. Choose the data type and identifier name appropriately. Write your answer in the form of a Java declaration.
- A Social Security Number (9 digits)
String socialSecurityNumber;
or
int socialSecurityNumber;
(String is better because it's easier to make a string contain 9 digits.)
- A bank account number that can contain letters and digits.
String accountNumber;
- The sum of the numbers from 1 to 100
int sum1to100;
- A constant representing the key on the keyboard that a user should type to signify "yes."
static final char YES_KEY;
(just char by itself is fine but this is the way you'd really do it.)
- The average temperature in degrees Fahrenheit in San Antonio.
float avgTempFSanAntonio;
- Write a code segment that randomly flips a coin by finding a random number between 0 and 1, printing "tails" for 0 and "heads" for 1.
- What is the output of the following code segment?
int i, j, k;
double a, b, c;
char x, y, z;
i = 10;
j = 100;
k = 5;
a = i;
b = j;
c = k;
x = 'a';
y = 'b';
z = 'c';
System.out.println (j / i);
System.out.println (j / i);
System.out.println (k % 2);
System.out.println (c / a);
System.out.println (b / c / a);
System.out.println (b / (c / a));
if (x >= y && z >= y)
System.out.println ("yes");
else
System.out.println ("no");
System.out.println (x < y || y > z);
Solution:
10
10
1
0.5
2.0
200.0
no
true
- Write a code segment that compares two strings s1 and s2 and prints "less than" if s1 comes before s1 in lexicographical order, "greater than" if s1 comes after s2 in lexicographical order, or "equal" if the two strings are equal.
Solution:
int c = s1.compareTo (s2);
if (c < 0)
System.out.println ("less than");
else if (c > 0)
System.out.println ("greater than");
else
System.out.println ("equal");
- We are writing a payroll program. A worker receives $10 per hour
for working up to 40 hours. For each additional hour over 40 hours up
to 60 hours, the worker receives "time and a half," i.e. $15 per hour.
For each additional hour over 60 hours, the worker receives "double time,"
i.e. $20 per hour. Assume that the integer variable hoursWorked
contains the number of hours the worker has worked. Write a code segment
that sets the float variable wages to the amount the worker
has earned. Note: the code should not do any input or output.
Solution:
if (hoursWorked <= 40)
wages = 10 * hoursWorked;
else if (hoursWorked <= 60)
wages = 10 * 40 + (hoursWorked - 40) * 15;
else
wages = 10 * 40 + 15 * 20 + (hoursWorked - 60) * 20;
- The integer variable temperature contains the temperature
of a container of water in degrees Celsius. Water freezes at 0°C.
Water boils at 100°C. Write a code segment that prints "liquid" if
the water is in the liquid state, "solid" if the water is cold enough to
freeze, or "gas" if the water is hot enough to boil.
Solution:
if (temperature <= 0)
System.out.println ("solid");
else if (temperature >= 100)
System.out.println ("gas");
else
System.out.println ("liquid");
- Write a code segment that prompts the user with "How many items do you
have?" and then reads an integer. If the number of items is at least 12,
the code should print "you have at least one dozen"; otherwise, it should
print "you have less than one dozen." Make sure to declare your variable(s)
with meaningful identifier name(s).
Solution:
System.out.print ("How many items do you have? ");
Scanner scan = new Scanner (System.in);
int numItems = scan.nextInt();
if (numItems >= 12)
System.out.println ("You have at least one dozen.");
else
System.out.println ("You have less than one dozen.");
- Write a code segment that prompts the user with "How many items do
you have?" and then reads an integer. If the number of items is more than
10, the code should print "you have too many items" and then go back to
the prompt. The code should continue this until the user enters a number
of items that is at most 10. Use a do/while loop.
Solution:
int numItems;
do {
System.out.print ("How many items do you have? ");
Scanner scan = new Scanner (System.in);
numItems = scan.nextInt();
if (numItems > 10)
System.out.println ("You have too many items.");
} while (numItems > 10);
- Write a code segment to print out all of the odd numbers between 0 and 100, inclusive.
Solution:
for (int odd=1; odd<=99; odd+=2)
System.out.println (odd);
- Write a code segment that declares average and sets it to
the average of the squares of the even numbers between 0 and 100, inclusive.
Solution:
int sum = 0, count = 0;
for (int even=0; even<=100; even+=2) {
sum += even;
count++;
}
double average = sum / (double) count;