CAT JAVA
CAT JAVA
CAT
Maximum:100 Marks
Instructions:
INSTRUCTORS
2. There are four platforms of the Java programming language.Tick all that apply
20.A constructor is a method which has the same name as the class, and no return
type. We can use it to initialise data members
a)Yes
b)No
21. Complete the following table using Yes and No./10 Marks(Marking: Missed item 0.5
Mark)
Access Modifier private protected default public
22. Encapsulate the Product class and overload its constructor at 3 times./10 Marks
Product{id, name, supplier, cost, manufactureDate}:
( Marking : Private Data members 3 Marks, Proper datatype choice 1 Mark, Public
getters and setters 3 Marks and overloading 3 Marks)
Sample Answer:
import java.time.LocalDate;
public class Product {
private int id;
private String name;
private String supplier;
private int cost;
private LocalDate manufacturedDate;
public Product() {
}
public Product(int id, String name, String supplier, int
cost, LocalDate manufacturedDate) {
super();
this.id = id;
this.name = name;
this.supplier = supplier;
this.cost = cost;
this.manufacturedDate = manufacturedDate;
}
public Product(String name, String supplier, int cost,
LocalDate manufacturedDate) {
this.name = name;
this.supplier = supplier;
this.cost = cost;
this.manufacturedDate = manufacturedDate;
}
// Other constructors can be added either by changing the
order of arguments or
// reducing the parameters
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSupplier() {
return supplier;
}
public void setSupplier(String supplier) {
this.supplier = supplier;
}
public int getCost() {
return cost;
}
public void setCost(int cost) {
this.cost = cost;
}
public LocalDate getManufacturedDate() {
return manufacturedDate;
}
public void setManufacturedDate(LocalDate
manufacturedDate) {
this.manufacturedDate = manufacturedDate;
}
// Added but not mandatory
@Override
public String toString() {
return "Product [id=" + id + ", name=" + name + ",
supplier=" + supplier + ", cost=" + cost
+ ", manufacturedDate=" + manufacturedDate
+ ", getId()=" + getId() + ", getName()=" + getName()
+ ", getSupplier()=" + getSupplier() + ",
getCost()=" + getCost() + ", getManufacturedDate()="
+ getManufacturedDate() + ", getClass()="
+ getClass() + ", hashCode()=" + hashCode() + ", toString()="
+ super.toString() + "]";
}
}
23. Create Person class with id, name, identity, dob and inherits it using Account class which
have accountNumber and balance attribute. The account class should have additional
methods which are deposit and withdraw which take balance as a parameter. The amount to
deposit should be positive and the amount to withdraw should be NOT greater than the
available balance.
Create a BankingApp class with the main method to test the deposit and withdraw methods
of the Account class./10 Marks
Sample Answer:(Marking class Person 3 Marks,class Account 5 Marks(use of extends
1 Mark, Good subclass constructor 2 Marks, Withdraw 1 Mark, Deposit 1 Mark,
Banking App 2 Marks),
class Person {
private int id;
private String name;
private String identity;//The student can use also other
types like integers
private LocalDate dob;//The student can use Date
//or
//protected int id;
//protected String name;
//protected String identity;
//protected LocalDate dob;
public Person() {
}
public Person(int id, String name, String identity,
LocalDate dob) {
super();
this.id = id;
this.name = name;
this.identity = identity;
this.dob = dob;
}
public Person(String name, String identity, LocalDate
dob) {
super();
this.name = name;
this.identity = identity;
this.dob = dob;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getIdentity() {
return identity;
}
public void setIdentity(String identity) {
this.identity = identity;
}
public LocalDate getDob() {
return dob;
}
public void setDob(LocalDate dob) {
this.dob = dob;
}
}
class Account extends Person{
private String account; //Account also can be integer
private double balance; //balance can also be integer
public Account() {}
public Account(int id, String name, String identity,
LocalDate dob, String account, double balance) {
super(id, name, identity, dob);
this.account = account;
this.balance = balance;
}
public Account( String name, String identity, LocalDate
dob, String account, double balance) {
super( name, identity, dob);
this.account = account;
this.balance = balance;
}
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public void withDraw(double balance) {
if(this.balance>=balance&& balance>0) {
this.balance-=balance;
}
else
return;
}
public void deposit(double balance) {
if(balance>0) {
this.balance+=balance;
}
else
return;
}
}
class BankingApp{
public static void main(String[]args) {
Account account1= new
Account(1,"Mugisha","111626G",LocalDate.now(),"20240909",200.5
);
account1.deposit(300);
account1.withDraw(100);
System.out.println(account1.getBalance());
}
}
Good Luck!!!!