Wednesday 19 February 2014

Java Programs to Solve:
==================
Question 1:
(The Triangle class) Design a class named Triangle that extends GeometricObject. The class
contains:
● Threedoubledata fields named side1,side2, and side3with default values 1.0 to denote
three sides of the triangle.
● A default constructor that creates a default triangle.
● A constructor that creates a triangle with the specified side1, side2, and side3.
● The accessor methods for all three data fields.
● A method named getArea() that returns the area of this triangle.
● A method named getPerimeter() that returns the perimeter of this triangle.
● A method named toString() that returns a string description for the triangle. For the
formula to compute the area of a triangle. The toString() method is implemented as
follows:
return "Triangle: side1 = " + side1 + " side2 = " + side2 + " side3 = " + side3;
public class SimpleGeometricObject {
private String color = "white";
private boolean filled;
private java.util.Date dateCreated;
/**
* Construct a default geometric object
*/
public SimpleGeometricObject() {
dateCreated = new java.util.Date();
}/
**
* Construct a geometric object with the specified color and filled value
*/
public SimpleGeometricObject(String color, boolean filled) {
dateCreated = new java.util.Date();
this.color = color;
this.filled = filled;
}/
**
* Return color
*/
public String getColor() {
return color;
}/**
* Set a new color
*/
public void setColor(String color) {
this.color = color;
}/
**
* Return filled. Since filled is boolean, its get method is named isFilled
*/
public boolean isFilled() {
return filled;
}/
**
* Set a new filled
*/
public void setFilled(boolean filled) {
this.filled = filled;
}/
**
* Get dateCreated
*/
public java.util.Date getDateCreated() {
return dateCreated;
}/
**
* Return a string representation of this object
*/
public String toString() {
return "created on " + dateCreated + "\ncolor: " + color
+ " and filled: " + filled;
}
}
Draw the UML diagrams for the classes Triangle and GeometricObject and implement the
classes. Write a test program that prompts the user to enter three sides of the triangle, a color,
and a Boolean value to indicate whether the triangle is filled. The program should create a
Triangle object with these sides and set the color and filled properties using the input. The
program should display the area, perimeter, color, and true or false to indicate whether it is filled
or not
Question 2:
(IntegerSet Class) Create class IntegerSet for which each object can hold integers in the
range 0 through 100. Represent the set internally as a vector of bool values. Element a[i] is true if
integer i is in the set. Element a[j] is false if integer j is not in the set. The default constructor
initializes a set to the socalled
“empty set,” i.e., a set for which all elements contain false.
● Provide member functions for the common set operations. For example, provide a
unionOfSets
member function that creates a third set that is the settheoretic
union of
two existing sets (i.e., an element of the result is set to true if that element is true in either
or both of the existing sets, and an element of the result is set to false if that element is
false in each of the existing sets).
● Provide an intersectionOfSets member function which creates a third set which is the
settheoretic intersection of two existing sets (i.e., an element of the result is set to false if
that element is false in either or both of the existing sets, and an element of the result is
set to true if that element is true in each of the existing sets).
● Provide an insertElement member function that places a new integer k into a set by
setting a[k] to true.
● Provide a deleteElement member function that deletes integer m by setting a[m] to false.
● Provide a printSet member function that prints a set as a list of numbers separated by
spaces.
● Print only those elements that are present in the set (i.e., their position in the vector has a
value of true). Print for
an empty set.
● Provide an isEqualTo member function that determines whether two sets are equal.
● Provide an additional constructor that receives an array of integers and the size of that
array and uses the array to initialize a set object. Now write a driver program to test your
IntegerSet class.
● Instantiate several IntegerSet objects.
● Test that all your member functions work properly.

CS480L_Week6
1. The Person Class
Implement the class Person
o Data members
1. age
2. name
o Member functuions
1. Manager function
a. Constructor
2. Access functions
a. 2 get functions
b. 2 set functions
c. Predicate
o isOld()
A person is old if his/her age is over 80.
Test your class by
 input the values of attributes
o Use dialog box to get a person's name
o Use the Scanner class to get a person's age
 Create a Person's object based on the name and age entered from keyboard.
2. The Account Class
Design a class named Account that contains:
 A private int data field named id for the account (default 0).
 A private double data field named balance for the account (default 0).
 A private double data field named annualInterestRate that stores the current
interest rate (default 0). Assume all accounts have the same interest rate.
 A private Date data field named dateCreated that stores the date when the
account was created.
 A no-arg constructor that creates a default account.
 A constructor that creates an account with the specified id and initial balance.
 The accessor and mutator methods for id, balance, and annualInterestRate.
 The accessor method for dateCreated.
CS480L_Week6
 A method named getMonthlyInterestRate() that returns the monthly interest
rate.
 A method named withdraw that withdraws a specified amount from the
account.
 A method named deposit that deposits a specified amount to the account.
Implement the class. Write a test program that creates an Account object with an
account ID of 1122, a balance of $20,000, and an annual interest rate of 4.5%. Use the
withdraw method to withdraw $2,500, use the deposit method to deposit $3,000, and
print the balance, the monthly interest, and the date when this account was created.
See code illsuration below on how Account class can be utilized.
public class TestAccountClass {
public static void main (String[] args) {
Account account = new Account(1122, 20000);
Account.setAnnualInterestRate(4.5);
account.withdraw(2500);
account.deposit(3000);
System.out.println("Balance is " + account.getBalance());
System.out.println("Monthly interest is " +
account.getMonthlyInterest());
System.out.println("This account was created at " +
account.getDateCreated());
}}

===================================
Question
Implement the following class diagrams in Java. Print out what tasks does an objectin the doWork() method. For example, a student object will do “studying”. Each display() method should print all the object’s data fields and its parent’s data fields. Each getInput() method should get all the inputs from keyboard and assign them to the object’s fields and its parent’s fields. Write a main function to test your classes.
Interface Displayable
+display():void
Interface Inputable
+getInput():void
Interface Workable
+doWork():void
Person
-name:string
-age:int
+Person(name, age)
Student
-program:string
-id:int
-gpa:double
-hands:Hand[]
+Student(What parameters?)
College
-name:string
-departments:Department[]
+College(what parameters?)
Professor
-specialty :string
-teachingYears:int
+Professor(what parameters)
Department
-name :string
-professors:Professor[]
-students:Students[]
+Department(what parameters?)
+addProfessor(professor)
+addStudent(student)
+removeProfessor(professor)
+removeStudent(student)
*
*