CS 1063 Intro. to Programming
|
import java.util.Scanner;
public class Digits {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Type an integer ---> ");
int digits = in.nextInt();
int tensDigit = digits / 10;
int unitsDigit = digits % 10;
System.out.println("Number: " + digits +
", Tens: " + tensDigit +
", Units: " + unitsDigit);
}
}
Output of three runs:
Type an integer ---> 47
Number: 47, Tens: 4, Units: 7
Type an integer ---> 8
Number: 8, Tens: 0, Units: 8
Type an integer ---> 92
Number: 92, Tens: 9, Units: 2
public class Testit {
public static void main(String[] args) {
int a = 89;
int b = 47;
System.out.println("a: " + a + ", b: " + b);
if (a > b) {
int temp = a;
a = b;
b = temp;
}
System.out.println("a: " + a + ", b: " + b);
}
}
Output of a run:
a: 89, b: 47
a: 47, b: 89
n = n + 1; n += 1; n++; ++n;
public class Testit2 {
public static void main(String[] args) {
String s1 = "Stephen";
String s2 = "Stephany";
if (s1.compareTo(s2) < 0) System.out.println("FIRST-BEFORE-SECOND");
else if (s1.compareTo(s2) > 0) System.out.println("FIRST-AFTER-SECOND");
else System.out.println("FIRST-EQUALS-SECOND");
}
}
Output of a run:
FIRST-AFTER-SECOND
public class Wages {
public static void main(String[] args) {
double hoursWorked = 50.0;
double hourlyRate = 10.0;
double wages;
if (hoursWorked <= 40.0) {
wages = hoursWorked*hourlyRate;
}
else { // hoursWorked > 40.0
wages = hourlyRate*40.0 +
(hoursWorked - 40.0)*hourlyRate*1.5;
}
System.out.println("Hours worked: " + hoursWorked +
", Hourly rate: " + hourlyRate +
", Wages: " + wages);
}
}
Output of a run:
Hours worked: 50.0, Hourly rate: 10.0, Wages: 550.0
Here's a slightly different method:
public class Wages2 {
public static void main(String[] args) {
double hoursWorked = 50.0;
double hourlyRate = 10.0;
double wages;
// first apply normal rate to all hours
wages = hoursWorked*hourlyRate;
// now add in extra half rate for hours > 40
if (hoursWorked > 40.0) {
wages = wages + (hoursWorked - 40.0)*hourlyRate*0.5;
}
System.out.println("Hours worked: " + hoursWorked +
", Hourly rate: " + hourlyRate +
", Wages: " + wages);
}
}
Output of a run:
Hours worked: 50.0, Hourly rate: 10.0, Wages: 550.0
if (grade >= 0 && grade <= 100) // note && for "and"
System.out.println("The number is valid");
if (hours < 0 || hours > 80) // note || for "or"
System.out.println("The number is not valid");
| points | Grade |
|---|---|
| greater than 100 | A+ |
| 90 to 100 inclusive | A |
| 80 to 89 inclusive | B |
| 70 to 79 inclusive | C |
| 60 to 79 inclusive | D |
| 0 to 59 inclusive | F |
| less than 0 | Invalid |
import java.util.Scanner;
public class Grades {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Type an integer ---> ");
int points = in.nextInt();
String grade = "";
if (points > 100) grade = "A+";
else if (points >= 90 && points <= 100) grade = "A";
else if (points >= 80 && points < 90) grade = "B";
else if (points >= 70 && points < 80) grade = "C";
else if (points >= 60 && points < 70) grade = "D";
else if (points >= 0 && points < 60) grade = "F";
else if (points < 0) grade = "Invalid";
System.out.println("Points: " + points +
", Grade: " + grade);
}
}
Output of multiple runs:
Type an integer ---> 101
Points: 101, Grade: A+
Type an integer ---> 100
Points: 100, Grade: A
Type an integer ---> 90
Points: 90, Grade: A
Type an integer ---> 89
Points: 89, Grade: B
Type an integer ---> 80
Points: 80, Grade: B
Type an integer ---> 60
Points: 60, Grade: D
Type an integer ---> 59
Points: 59, Grade: F
Type an integer ---> 0
Points: 0, Grade: F
Type an integer ---> -1
Points: -1, Grade: Invalid
You have prepared for this question by having done the labs. The question will be similar to these labs, but simpler. You can also prepare by looking over or working out the two extra review classes in the Week 12 Review: the Triangle class, and the RomanNumeral class. (Note that the switch statement in the answer to the question about the RomanNumeral class in not required. It can easily be done with an extended if-else statement.)