Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
Chapter 7: Introduction to Classes and Objects – Exercises
1. What is the purpose of the keyword new?  Explain what happens when you use it.
 
2. What is a default constructor?  How are an object’s instance variables initialized if a class 
has only a default constructor?
 
3. Explain the purpose of an instance variable.
 
4. Most classes need to imported before they can be used in an application.  Why is every 
application allowed to use classes System and String without first importing them?
 
5. Explain how a program could use class Scanner without importing the class.
 
6. Explain why a class might provide a set method and a get method for an instance variable.
 
7. Create an Employee class that includes a first name (type String), a last name (type String) 
and a monthly salary (double) instance variable.  Provide a constructor that initializes the 
three instance variables.  Provide a set and get method for each instance variable.  If the 
monthly salary is not positive, do not set its value.  Write a test application names 
EmployeeTest that demonstrates class Employee’s capabilities.  Create two Employee 
objects and display each object’s yearly salary.  Then give each Employee a 10% raise and 
display each Employee’s yearly salary again
 
 
Chapter 8:  Classes and Objects: A Deeper Look – Programming Exercises
1. Create a class Rectangle with attributes length and width, each of which defaults to 1.  
Provide methods that calculate the rectangle’s perimeter and area.  It has set and get 
methods for both length and width.  The set methods should verify that length and width are 
each floating-point numbers larger than 0.0 and less than 20.0.  Write a program to test 
class Rectangle.
 
2. Create class SavingsAccount.  Use a static variable annualInterestRate to store the annual 
interest rate for all account holders.  Each object of the class contains a private instance 
variable savingsBalance indicating the amount the saver currently has on deposit.  Provide 
method calculateMonthlyInterest to calculate the monthly interest by multiplying the 
savingsBalance by annualInterestRate divided by 12 – this interest should be added to 
savingsBalance.  Provide a static method modifyInterestRate that sets the 
annualInterestRate to a new value.  Write a program to test class SavingsAccount.  
Instatiate two savingsAccount objects, saver1 and saver2, with balances of $2000.00 and 
$3000.00, respectively.  Set annualInterestRate to 4%, then calculate the monthly interest 
for each of 12 months and print the new balances for both savers. Next, set the 
annualInterestRate to 5%, calculate the next month’s interest and print the new balances for 
both savers. 
 
Chapter 9: Object-Oriented Programming: Inheritance – Exercises
1. Discuss the ways in which inheritance promotes software reuse, saves time during program 
development and helps prevent errors.
 
2. Some programmers prefer not to use protected access, because they believe it breaks the 
encapsulation of the superclass.  Discuss the relative merits of using protected access vs. 
using private access in superclasses.
 
3. Write an inheritance hierarchy for class Quadrilateral, Trapezoid, Parallelogram, Rectangle 
and Square.  Use Quadrilateral as the superclass of the hierarchy.  Create and use a Point 
class to represent the points in the shape.  Make the hierarchy as deep (i.e., as many levels)
as possible.  Specify the instance variables and methods for each class.  The private 
instance variables of Quadrilateral should be the x-y coordinate pairs for the four endpoints 
of the Quadrilateral.  Write a program that instantiates objects of your classes and outputs 
each object’s area (except Quadrilateral).