8. Algorithm Workbench 1-4 1, Design a class named pet, which should have the fo
ID: 3540929 • Letter: 8
Question
8. Algorithm Workbench 1-4
1, Design a class named pet, which should have the following fields:
* name. The name field holds the name of a pet.
* animal. The animal field holds the type of animal that a pet is. Example values are "Dog","Cat",and "Brid".
* age. The age field holds the pet's age.
The pet class should also have the following methods:
* setName. The setName method stores a value in the field.
* setAnimal. The setAnimal method stores a value in the animal field.
* setAge. The setAge method stores value in the age field.
* getName. The getName method returns the value of the name field.
* getAnimal. The getAnimal method returns the value of the animal field.
* getAge. The getAge method returns the value of the age field.
a. Draw a UML diagram of the class. Be sure to inculde notation showing each field and method's access specification and data type. Also inculde notation showing any method parameters and their data types.
b. Write the Java code for the Pet class.
2,Look at the following partial class definition, and then respond to the questions that following it:
public class Book
{
private String title;
private String author;
private String publisher;
private int copiesSold;
}
a,Write a constructor for this class. the constructor should accept an argument for each of the fields.
b,Write accessor and mutator methods for each field.
c,Draw a UML diagram for the class, including the methods you have written.
3, Consider the following class delaration:
public class Square
{
private double sideLength;
public double getArea()
{
return sideLength * sideLength;
}
public double getSideLength()
{
return sidelength;
}
}
a. Write no-arg constructor for this class. It should assign the sidelength field the value 0.0.
b. Write an overloaded constructor for this class. It should accept an argument that is copied into the sideLength field.
4, Look at the following description of a problem domain:
The bank offers the following types of accounts to its customers: savings accounts,checking accounts,and money market accounts. customers are allowed to deposit money into an account (thereby increasing its balance), withdraw money from an account (thereby decreasing its balance), and earn interest on the account. Each account has an interest rate.
Assume that you are writing an application that will calculate the amount of inerest earned for a bank account.
a, Identify the potential classes in this problem domain.
b, Refine the list to inculde only the necessary class or classes for this problem.
c, Identify the responsibilities of the class or classes.
Explanation / Answer
1b. Code
public class Pet {
private String name;
private String animal;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAnimal() {
return animal;
}
public void setAnimal(String animal) {
this.animal = animal;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
2a. public Book(String title, String author, String publisher, int copiesSold) {
super();
this.title = title;
this.author = author;
this.publisher = publisher;
this.copiesSold = copiesSold;
}
To add values to these field, we can create an object of Book class with these parameters.
Book book = new Book("Head First Java","Keithy Siera","O'Reily", 2000);
This object named book ll have all the details populated.
2b. public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getPublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
public int getCopiesSold() {
return copiesSold;
}
public void setCopiesSold(int copiesSold) {
this.copiesSold = copiesSold;
}
3a.
public Square() {
sideLength = 0.0;
}
3b.
public Square(double sideLength) {
this.sideLength = sideLength;
}
4.
I would prefer to make two classes
1. Bank
2. BankTest
responsiblities
Bank class will have list of customers stored. It ll have necessary fields such as
customerId (which ll give the id of the customer whose account is there in the bank. So that his/her necessary details can be fetched)
balance(to show the present balance of a customer on the basis of the customerId)
withdrawlAmount (the amount to be withdrawn)
depositAmount (the amount to be deposited)
accountType(it will be either of three i.e savings accounts,checking accounts,and money market accounts. ).
interestRateSavings (different account types may or may not have different interest rates. But it is advisable to keep different interest rate)
interestRateChecking (interest rate of checking accounts)
interestRateMarket( interest rate of money market accounts)
methods
a method to deposit money, where amount to be deposited will be taken as parameter and added in the account balance of the customer.
a method to withdraw money, where money to be withdrawn will be subtracted from the account balance of the customer. This method will also check if the customer has enough balance to withdraw the money.
a method to calculate the interest if a customer has kept his/her money in the bank
a method to show present account balance of a customer
BankTest class will have the main () method and nothing else.
this method ll call all the methods of Bank class as per need and show the details of the transaction on console.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.