Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

More On Classes: Workshop 4 in This Workshop, You'll Learn

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

More on classes

Workshop 4

In this workshop, you’ll learn:

 What is encapsulation?
 Organize classes in packages
 Information hiding: Access modifiers and others

1. Given a class Book defined as having the following attributes:


Author author;
String title;
int noOfPages;
boolean fiction;
Actually code and compile the Book class based on the attributes and “get”/“set” methods
called. Write a main program to test all the above operations.
2. Given a class Student defined as having the following attributes:
String name;
String studentId;
Date birthDate;
String address;
String major;
double gpa;
Actually code and compile the Student classes as follows:
• Include accessor methods for every attribute.
• Reflect the appropriate accessibility on all features.
• Include one or more constructors for class.
• Write a method with the header public void printAllAttributes() that can be used to
display the values of all attributes to the command prompt, for example:
Student Name: John Smith

1
Student ID: 123-45-6789
etc.
Then, modify the accompanying Main class’s main method to take advantage of your new constructors
to instantiate one of each of the object types.
Additional proposed exercises:
3.
Write a Rectangle class to represent rectangles, with two data fields (width, height) and four methods
(input data, display data, calculate and return area, calculate and return perimeter). Write another class
to test all methods of the Rectangle class.
4.
Write java program to manage fractions with addition, subtraction, multiplication and division.

You might also like