Directions: Fill in answers on the pages below. Don't spend too much time on any one problem.
| income | taxRate |
|---|---|
| <= 10000 | 0 |
| > 10000 and <= 50000 | .20 |
| > 50000 and <= 200000 | .35 |
| > 200000 | .50 |
Answers are in red below.
| Income.java | One run |
|---|---|
import java.util.Scanner;
public class Income {
public static void main(String[] args) {
int income;
double taxRate;
Scanner in = new Scanner(System.in);
income = in.nextInt();
while (income >= 0) {
// put if-else statement(s) here to
// give taxRate the proper value
if (income <= 10000) taxRate = 0.0;
else if (income <= 50000) taxRate = 0.2;
else if (income <= 200000) taxRate = 0.35;
else taxRate = 0.5;
System.out.println("income: " + income +
", taxRate: " + taxRate);
// statement here to give tax the proper value
double tax = income*taxRate;
// statements here to print the proper message
if (tax > 0.0)
System.out.println("You owe " + tax);
else
System.out.println("You owe nothing");
income = in.nextInt();
}
}
}
| 9999 income: 9999, taxRate: 0.0 You owe nothing 10000 income: 10000, taxRate: 0.0 You owe nothing 10001 income: 10001, taxRate: 0.2 You owe 2000.2 50000 income: 50000, taxRate: 0.2 You owe 10000.0 50001 income: 50001, taxRate: 0.35 You owe 17500.35 100000 income: 100000, taxRate: 0.35 You owe 35000.0 200000 income: 200000, taxRate: 0.35 You owe 70000.0 200001 income: 200001, taxRate: 0.5 You owe 100000.5 0 income: 0, taxRate: 0.0 You owe nothing -1 |
int k = 2;
while (k <= 20) {
System.out.print(k + " ");
k = k + 2;
}
System.out.println();
for (int i = 1; i < 6; i++) {
System.out.println(i + " squared = " + i*i);
}
// for loop as a while below
int i = 1;
while (i < 6) {
System.out.println(i + " squared = " + i*i);
i++;
}
// what the loop will print below
1 squared = 1
2 squared = 4
3 squared = 9
4 squared = 16
5 squared = 25
public class Testit {
public static void main(String[] args) {
int sum = 0;
for (int i = 1; i <= 100; i++)
sum = sum + i;
System.out.println(sum);
}
}
Output: 5050
public class Student {
private String myName; // Student's name
private int myNumberOfCourseHours; // number of course hours
private int myTotalGradePoints; // total grade points
// put constructor below (Part a)
public Student(String name) {
// new student, with name, but no quizzes
myName = name;
myNumberOfCourseHours = 0;
myTotalGradePoints = 0;
}
// put accessor method for myName below (Part b)
public String getName() {
// return the student's name
return myName;
}
public int getNumberOfCourseHours() {
return myNumberOfCourseHours;
}
public int getTotalGradePoints() {
return myTotalGradePoints;
}
public void addSemester(int courseHoursTaken, int gradePointsEarned) {
if (courseHoursTaken <= 0)
System.out.println("Incorrect input data\n");
else {
myNumberOfCourseHours = myNumberOfCourseHours + courseHoursTaken;
myTotalGradePoints = myTotalGradePoints + gradePointsEarned;
}
}
// put definition of helper method getGPA below (Part c)
public double getGPA() {
if (myNumberOfCourseHours == 0)
return -1.0;
else
return (double)myTotalGradePoints/myNumberOfCourseHours;
}
public String toString() {
return "Name: " + getName() +
"\n course hours: " + getNumberOfCourseHours() +
"\n grade points: " + getTotalGradePoints() +
"\n grade point average: " + getGPA();
}
}
//----------------------------------------------------------
public class StudentTest {
public static void main(String[] args) {
// put answers to Part d below
Student frank = new Student("Frank Griswold");
System.out.println(frank);
frank.addSemester(6, 15);
System.out.println(frank);
System.out.println("Frank's GPA: " + frank.getGPA());
frank.addSemester(9, 24);
System.out.println(frank);
}
}
Output:
Name: Frank Griswold
course hours: 0
grade points: 0
grade point average: -1.0
Name: Frank Griswold
course hours: 6
grade points: 15
grade point average: 2.5
Frank's GPA: 2.5
Name: Frank Griswold
course hours: 15
grade points: 39
grade point average: 2.6
| Date.java | DateTest.java, plus run |
|---|---|
// Date: represent dates
public class Date {
private int myDay; // 1-28, 30, 31
private int myMonth; // 1-12
private int myYear; // 1900-2100
public Date(int day, int month, int year) {
// if error, set field to 0
if (day < 1 || day > daysInMonth(month))
day = 0;
myDay = day;
if (month < 1 || month > 12) month = 0;
myMonth = month;
if (year < 1900 || year > 2100) year = 0;
myYear = year;
}
private int daysInMonth(int month) {
// incorrect on month == 2 in leap year
if (month == 2) return 28;
if (month == 4 || month == 6 ||
month == 9 || month == 11) return 30;
return 31;
}
public int getDay() { return myDay; }
public int getMonth() { return myMonth; }
public int getYear() { return myYear; }
public String toString() {
String res = "";
// put in leading 0 on day
if (myDay < 10) res = res + "0";
res = res + myDay;
res = res + "/";
// put in leading 0 on month
if (myMonth < 10) res = res + "0";
res = res + myMonth;
res = res + "/";
res = res + myYear;
return res;
}
public void nextDay() {
if (myDay == daysInMonth(myMonth)) {
myDay = 1;
myMonth++;
}
else myDay++;
// in case above incremented myMonth
if (myMonth == 13) {
myMonth = 1;
myYear++;
}
}
}
| public class DateTest {
public static void main(String[] args) {
Date today = new Date(13, 12, 2005);
System.out.println(today);
today.nextDay();
System.out.println(today);
System.out.println();
Date newYearsEve =
new Date(31, 12, 2005);
System.out.println(newYearsEve);
newYearsEve.nextDay();
System.out.println(newYearsEve);
System.out.println();
Date endOfFeb = new Date(28, 2, 1998);
System.out.println(endOfFeb);
endOfFeb.nextDay();
System.out.println(endOfFeb);
System.out.println();
Date testDate = new Date(29, 2, 2100);
System.out.println(testDate);
testDate.nextDay();
System.out.println(testDate);
System.out.println();
}
}
// a run of this program
13/12/2005
14/12/2005
31/12/2005
01/01/2006
28/02/1998
01/03/1998
00/02/2100
01/02/2100
|
Points for each problem: 1-30, 2-20, 3-25, 4-25, 5-50, 6-50.