Solution Manual for Objects First with Java: A Practical Introduction Using BlueJ, 6th Edition
- Information Technology
- 126 pages
- Shared May 2025
Solution Manual for Objects First with Java: A Practical Introduction Using BlueJ, 6th Edition makes solving textbook exercises easier with step-by-step solutions and helpful tips.
Preview 16 of 126 pages
See the first 16 pages exactly as they appear in the document.
Page 1 Read page 1 as text
Exercise 1.3 Use a negative parameter value to move left, e.g. - 70 Exercise 1.9 The House Picture The main building: • Create a new Square object • Invoke its method makeVisible() • Make the square bigger by invoking the method changeSize(newSize) (100 is a good size) • Move the square down by invoking the method moveVertical(distance) (again 80 is a good value) The window: • Create a new Square object. • Invoke its method makeVisible() • Change its color by invoking changeColor() • write "black" in the popupwindow • Move the square down by invoking the method moveVertical(distance) (100 is a good value) • Move it to the right by invoking moveRight() The roof: • Create a new triangle object. • Invoke its method makeVisible() • Change its size with changeSize(newHeight, newWidth) (50,140) • moveVertical(70) • moveHoizontal(60) The sun: • Create new Circle object. • Invoke its method makeVisible() • Change its color by invoking changeColor() (write "yellow" in the popup window) Optionally change its size with changeSize(60) • Move it to the right by invoking moveHorizontal(180) The Hilltop Picture The hill:
Page 2 Page 3 Read page 3 as text
• Create a new Circle object. • Invoke its makeVisible() method. • Change its color by invoking changeColor (write “green” in the popup window) Change its size with changeSize to something like 1000. • Move it left with moveHorizontal(-500). • Move it down with moveVertical(125). The sun: • Create a Circle. • Make it visible. • Set its size to 30. • Move it right by 150 pixels. • Move it down by 50 pixels. The larger figure: • Create a new Person object. • Invoke its makeVisible method. • Change its size to something like 50 high and 25 wide. • Move it left by 30 pixels and up by around 8. The smaller figure: • The size should be around 40 by 20. • Move it left by 3 pixels and down by 2. Exercise 1.1 4 • It uses the objects of the classes Circle, Square and Triangle. • It then moves these objects to the right places and changes the sizes and colors of the objects. Essentially calling the same methods as used in exercise 1.9 Exercise 1.16 Change: sun.changeColor("yellow"); to be: sun.changeColor("blue"); Note that this change should be made in both the draw() and setColor() methods. Students often forget to do it in the latter. Illustrate the problem by drawing the
Page 4 Read page 4 as text
picture, then calling setBlackAndWhite() then setColor() – the sun will have been changed to yellow, rather than blue, if the second change is not made. Exercise 1.17 The second sun will need to be positioned somewhere different from the first sun to be visible. As with the previous exercise, it is common for students to miss the need to change the setColor and setBlackAndWhite methods for compatibility with the addition. This is an earlier introduction to the need for regression testing ! Exercise 1.1 8 After the line sun.makeVisible() insert the following: • sun.slowMoveVertical(250); • Compile the Picture class (Press compile in the editor window) • Create instance of class Picture and invoke its draw() method. Exercise 1.19 Remove the line (if added in the previous exercise): slowMoveVertical(250); Right below the last } after the draw() method, add the sunset() method : /** * Animates the sunset. */ public void sunset() { sun.slowMoveVertical(250); } Compile! And run it. Exercise 1.20 Define a new field: private Person person; Initialize and position them in the draw(): person = new Person(); person.changeSize(80, 40); // Place them at ground level. person.moveVertical(15); // Make sure they are to the right of the house to start. person.moveHorizontal(200); Make them visible and move up to the house in sunset(): person.makeVisible(); // Walk up to the house. person.slowMoveHorizontal(-150);
Page 5 Read page 5 as text
Exercise 1. 22 When calling the method getName(), the name of the student is displayed in a popup window. The name displayed is the one typed in when the object was created. Exercise 1.2 4 It shows the number of students in the LabClass which is zero. Exercise 1. 31 Students looking these values up in the tutorial might not be able to readily identify that the integer types used here are int, as opposed to byte, short or long. Similarly, they may well suggest float rather than double for the final one. Be sure to point out that String always has an initial upper-case letter, because these subtleties are often missed. 0 int "hello" String 101 int -1 int true boolean "33" String 3.1415 double Exercise 1. 32 First you would have to decide which type the field should have. String would be a good type to hold a name, so we add the following line to the source file of Circle: private String name; The above line could be placed after this line in the source code of the Circle class: private boolean isVisib le; Exercise 1. 33 public void send(String msg) The name msg is arbitrarily chosen. Exercise 1.3 4 public int average(int firstNumber, int secondNumber) Exercise 1.3 5 The book is an object because it is a specific instance of the Book class.
Page 6 Read page 6 as text
Exercise 1 .3 6 Yes, an object can belong to several classes. One of the more famous examples is the platypus, which is both a mammal and egg-laying.
Page 7 Read page 7 as text
Exercise 2.2 Zero. Exercise 2.3 If too much money is inserted the machine takes it all - no refund. If there isn't enough money inserted, it still prints out the ticket. Exercise 2.5 It looks almost completely the same. Only the price on the ticket is different. Exercise 2.6 The outer part of the student class: public class Student { } The outer part of the LabClass class: public class LabClass { } Exercise 2.7 Yes, the order of public and class matters. Exercise 2.8 It is possible to leave out the word public . Exercise 2.9 It is not possible to leave out the word class . Exercise 2.10 Fields: price balance total Constructors: TicketMachine
Page 8 Read page 8 as text
Methods: getPrice getBalance insertMoney printTicket Exercise 2.11 It does not have any return type. The name of the constructor is the same as the name of the class. Exercise 2.12 int Student Server Exercise 2.13 alive tutor game Exercise 2.14 Student , Server , Person and Game Exercise 2.15 The exact order matters. Exercise 2.16 Yes, it always necessary to have a semicolon after a field declaration . Exercise 2.17 private int status; Exercise 2.18 It belongs to the class Student. Exercise 2.19 It has two parameters. One is of type String and the other of type double. Exercise 2.20 It would be reasonable to expect the types to be the same as the two parameters (String and double).
Page 9 Read page 9 as text
Can't really assume anything about the names, but probably something like title and price. Exercise 2.21 name = petsName; Exercise 2.22 public Date(String month, int day, int year) Exercise 2.23 Aside from their names, the only difference is that getPrice() returns the value of the price field whereas getBalance() returns the value of the balance field. Exercise 2.24 How much money have I inserted into the machine? Exercise 2.25 No. There is no direct link between the name of a method and the name of the field. However, it is a convention to use a name for the method that clearly links it to the field. Exercise 2.26 public int getTotal() { return total; } Exercise 2.27 Missing return statement. Exercise 2.28 The header for getPrice() has an int as return type. The header for printTicket() has void as return type. Exercise 2.29 No. Because they don't need to return anything. Yes. Both headers have void as return types. Exercise 2.31 It has a return type (void) and constructors do not have return types. It also does not have the same name as the class it is in.
Page 10 Read page 10 as text
Exercise 2.32 price = cost; Exercise 2.33 score = score + points; Exercise 2.34 It is a mutator. Use an inspector to view the current score, then call the increase method with a positive parameter value and observe that score increases by that value. Alternatively, if score has an accessor method; call the accessor, then call increase , and then call the accessor once again to verify that it returns the updated value, indicating that the field has been modified. Exercise 2.35 price = price - amount; Exercise 2.36 Note that no quote marks are printed, just the following: My cat has green eyes. Exercise 2.37 public void prompt() { System.out.println("Please insert the correct amount of money."); } Exercise 2.38 Instead of printing out the actual price of the ticket, it displays the word "price": # price cents. Exercise 2.39 Prints out the exact same string as in exercise 2.38 Exercise 2.40 No, because neither uses the value of the price field in the println statement. Exercise 2.41 public void showPrice() { System.out.println("The price of a ticket is " + price + " cents.");
Page 11 Read page 11 as text
} Exercise 2.42 They display different prices. This is because each ticket machine object has its own price field. The price that was set in one ticket machine does not affect the other ticket machine’s price. The unique value of each ticket machine's price field is substituted into the println statement when the method is called. Exercise 2.43 public TicketMachine() { price = 1000; balance = 0; total = 0; } When constructing a TicketMaching object you will not be prompted for a parameter value. The tickets always have a price of 1000 cents. Exercise 2.44 public TicketMachine() { price = 1000; balance = 0; total = 0; } public TicketMachine(int ticketCost) { price = ticketCost; balance = 0; total = 0; } Exercise 2.45 public void empty() { total = 0; } It needs no parameters. It is a mutator. Exercise 2.46 The balance does not change when an error message is printed. Inserting zero results in an error message. Exercise 2.47
Page 12 Read page 12 as text
This version does not print an error message when zero is inserted. Other than that, it does not change the observable behavior of the method. Exercise 2.48 Note the importance of using <= rather than < in the rewritten condition. if(amount <= 0) { System.out.println("Use a positive amount rather than: " + amount); } else { balance = balance + amount; } Exercise 2.49 The field is: isVisible. It determines whether the circle was visible or not. Yes. As a circle is either visible or not, only two states (values) are needed. Exercise 2.50 In the printTicket method of Code 2.8 the total is increased only by the price of the ticket, and not the full balance. The balance is then decreased by the price. Exercise 2.51 The else and associated block can be removed – if statements do not have to have an else part. If an illegal amount is used, no error message is printed but the method otherwise works correctly. Exercise 2.52 It could never become negative. The value in balance is checked in printTicket to ensure that it is always at least as large as price, so when price is subtracted balance cannot become negative. Exercise 2.54 saving = price * discount; Exercise 2.55 mean = total / count; Exercise 2.56 if(price > budget) { System.out.println("Too expensive."); } else { System.out.println("Just right."); } Exercise 2.57
Page 13 Read page 13 as text
if(price > budget) { System.out.println("Too expensive. Your budget is only: " + budget); } else { System.out.println("Just right."); } Exercise 2.58 Because balance is set to zero and then this new value is returned rather than the old one. The method will always return zero. It can be tested by inserting an amount, and then calling refundBalance(). The original would then return the amount inserted, but the new method returns 0. Exercise 2.59 An error is printed: unreachable statement. A return statement ends (exits) the method. Code after a return statement can therefore never be executed. Exercise 2.60 The variable price is being re-declared as a local variable in the constructor – this is the effect of the word int in front of it. This local variable ‘hides’ the field of the same name. So the ticket price is never assigned to the price field. Exercise 2.61 public int emptyMachine() { int oldTotal = total; total = 0; return oldTotal; } Exercise 2.62 public void printTicket() { int amountLeftToPay = price - balance; if(amountLeftToPay <= 0) { // Simulate the printing of a ticket. System.out.println("##################"); System.out.println("# The BlueJ Line"); System.out.println("# Ticket"); System.out.println("# " + price + " cents."); System.out.println("##################"); System.out.println(); // Update the total collected with the price. total += price; // Reduce the balance by the prince. balance -= price; } else { System.out.println("You must insert at least: " + amountLeftToPay + " more cents."); }
Page 14 Read page 14 as text
} Exercise 2.63 You would need fields to store the prices of each of the tickets that the machine can issue. You would need a method to select which type of ticket you would want. It will not be necessary to modify many of the existing methods, if the price field is updated each time you select a new ticket type. You would probably need to modify the constructor, to allow several ticket prices. Exercise 2.64 Name: getCode Return type: String Exercise 2.65 Name: setCredits Parameter name: creditValue Parameter type: int Exercise 2.66 public class Person { } Exercise 2.67 private String name; private int age; private String code; private int credits; Exercise 2.68 public Module(String moduleCode) { code = moduleCode; } Exercise 2.69 public Person(String myName, int myAge) { name = myName; age = myAge; } Exercise 2.70
Page 15 Read page 15 as text
The return type should not be void. public int getAge() { return age; } Exercise 2.71 public String getName() { return name; } Exercise 2.72 public void setAge(int newAge) { age = newAge; } Exercise 2.73 public void printDetails() { System.out.println("The name of this person is " + name); } Exercise 2.74 student1 : Student name: Benjamin Jonson id: 738321 credits: 0 Exercise 2.75 "Henr557" Exercise 2.76 It opens the editor with the source code for the Student class. It displays a message: StringIndexOutOfBoundsException This happens because the method getLoginName expects the name to be at least 4 characters long and "djb" is only 3 characters. Exercise 2.77 public Student(String fullName, String studentID) { if(fullName.length() < 4) { System.out.println("Error! The name should be at least 4 characters long"); } if(studentID.length() < 3) {
Page 16 Read page 16 as text
System.out.println("Error! The studentID should be at least 3 characters long"); } name = fullName; id = studentID; credits = 0; } Exercise 2.78 public String getLoginName() { String loginNamePart; if(name.length() < 4) { loginNamePart = name; } else { loginNamePart = name.substring(0,4); } String loginIdPart; if(id.length() < 3) { loginIdPart = id; } else { loginIdPart = id.substring(0,3); } return loginNamePart + loginIdPart ; } Exercise 2.79 102 "catfish" "cat9" "12cat" "cat39" "f" StringIndexOutOfBoundsException Exercise 2.80 The first call returns 0 and the second returns 500. Exercise 2.81 Because t2 refers to the same object as t1, the call will print 500. This example of aliasing is an important one and students should try to ensure that they understand what is going on here. Exercise 2.82 The call returns 1000. Even though the change was made via t1, because t2 is referring to the same object, it sees the new value. Note that we have only created a single TicketMachine object in these two exercises, but two variables refer to that one object. Exercise 2.83
110 more pages in the full document
Unlock it once and it stays in your library, ready to chat with or turn into flashcards.
Unlock the full documentMore Information Technology study materials
See all
Exam GuidesTest Bank for Managing and Using Information Systems: A Strategic Approach, 7th Edition (Chapters 1-13)
Information Technology
452 pages149downloadsFeb 2025
Exam GuidesTest Bank for Essentials of MIS, 12th Edition
Information Technology
342 pages146downloadsFeb 2025
Exam GuidesA+ Guide to Hardware, 9th Edition Test Bank
Information Technology
116 pages142downloadsFeb 2025
Textbook GuidesSolution Manual for Essentials of System Analysis and Design, 4th Edition
Information Technology
282 pages140downloadsFeb 2025
Textbook GuidesGuide To Networking Essentials, 7th Edition Solution Manual
Information Technology
48 pages139downloadsFeb 2025
Past Exams2023-2024 SOPHOS FIREWALL V19.5 Practice Exam with Answers (75 Solved Questions)
Information Technology
7 pages134downloadsFeb 2025
Class NotesLecture Notes for CCNP Enterprise: Core Networking (ENCOR) v8 Lab Manual, 2nd Edition
Information Technology
1089 pages123downloadsFeb 2025
Class NotesLecture Notes for Adobe XD Classroom in a Book (2020 release)
Information Technology
66 pages118downloadsFeb 2025
Study this document with CramX
Once it is in your library, every tool can work from it.

