Chapter 3 Assignments

doc

School

Houston Community College *

*We aren’t endorsed by this school

Course

6182-58114

Subject

Computer Science

Date

Feb 20, 2024

Type

doc

Pages

4

Uploaded by CorporalBookPheasant38

Report
Chapter 3 Assignment Debugging Exercises. 1. Try to debug each program without using Java. 2. Then, (a) copy each program to TextPad; (b) save each program as a Java file (in the “Save As” window, change the “Save as type” on the 3 rd line from the bottom of the window from “All Files” to “Java”, using the dropdown arrow; (c) change the “File name” to the name of the program; the name of the first program is “DebugThree1”; (d) fix and run the program. First Debug Program: (Be sure that your output is complete and makes sense) // This class calculates a waitperson's tip as 15% of the bill public class DebugThree1 { public static void main(String args[]) { double myCheck = 50.00; double yourCheck = 19.95; System.out.println("Tips are"); calcTip(mycheck); calctip(yourCheck); } public static int calcTip(int bill) //int double { final double RATE = 0.15; tip = bill + RATE; System.out.println("The tip should be at least " + tip); } } Second Debug Program: // This application displays some math facts public class DebugThree2 { public static void main(String args[]) { int a = 2, b = 5, c = 10; add(a, b); add(b, c); subtract(c, a); } public static void add( int a, int b ) { System.out.println("The sum of " + a + and + b + is + a + b); } public void subtract( int a, int b ) { System.out.println("The difference between " + 1
a + and + b + is + a - b); } } Third Debug Program: // This program calculates tutition bills as credits times rate per credit hour public class DebugThree3 { public static void main(String args[]) { int myCredits = 13 ; int yourCredits = 17 ; double rate = 75.84 ; System.out.println("My tuition:"); tuitionBill(myCredits, rate); System.out.println("Your tuition:"); tuitionBill(myCredits, rate); } public static void tuitionBill( int myCredits, float rate) { System.out.println("Total due " + (rate*credits)); } } Case 1: 1. Do the following:   a. Create a class named Eggs. Its main() method has one integer variable named numberOfEggs. Prompt the user to enter the number of eggs and assign the value to numberOfEggs. (Remember that you will need to convert the String the user enters to an int before assigning it to numberOfEggs.) Save the application as Eggs.java. b. Create a method to which you pass numberOfEggs and which will then calculate and display the number of dozens. Name the method appropriately. In this method, calculate the number of eggs in dozens; for example, 50 eggs is 4 full dozen (with 2 eggs remaining). Display "The number of dozens is XX", where XX is the actual number of FULL dozens of eggs. c. In your main method, call the method that you just created, and pass it numberOfEggs. d. puExecute your application twice. The first time, enter 13 at the prompt. The second time, enter 44 at the prompt. 2
Answer: Import java.util.Scanner; public class Eggs{ public static void main(String[] args){ Scanner s = new Scanner(System.in); System.out.println("Enter the number of eggs: "); String input = s.nextLine(); int numberOfEggs = Integer.parseInt(input); calculateDozenEggs(numberOfEggs); s.close(); } public static void calculateDozenEggs(int numberOfEggs){ int dozens = numberOfEggs/12; System.out.println("The number of dozens is " + dozens); } } Case 2: 1a. Create a class named Student. This class has attributes (fields) for an ID number, number of credit hours earned, and number of points earned. (For example, many schools compute grade point averages based on a scale of 4, so a three-credit-hour class in which a student earns an A is worth 12 points.) Include an attribute for grade point average. Include a method that calculates the grade point average (equal to number of points earned divided by credit hours earned), and then displays the student number and grade point average in the following format: "The GPA for Student Number XXX is YYY". Note that XXX is the actual student number and YYY is the actual grade point average. Though credit hours earned and points earned will always be in whole (integer) numbers, make sure that you are able to calculate and display GPAs with decimal positions (e.g., 3.1). 1b. Add a class named ShowStudent that does the following. (1) It creates three instances of the Student class. (2) It assigns the following attribute values for the three instances: Instance CreditHours PointsEarned StudentID first 30 120 1234 second 64 200 9876 3
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
third (Do not assign: use constructor default) (Do not assign: use constructor default) (Do not assign: use constructor default) (3) It calls each instance's method that displays the student's information. Compile and execute your programs. The following should be displayed: Why did the Exception message appear? 1c. Create a constructor method for the student class you created. In this constructor method, initialize studentID to 9999 and credit hours earned to 1. Compile and execute your programs. The following should be displayed: Answer: public class Student{ public static void main(String[] args){ int studentID; int pointEarned; int creditEarned; } } class ShowStudent { } 4