0% found this document useful (0 votes)
13 views

Java Predict

Uploaded by

victorkanoti
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Java Predict

Uploaded by

victorkanoti
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Question One: Object-Oriented Programming (30 Marks)

(a) Define Object-Oriented Programming (2 Marks)

Answer:
Object-Oriented Programming (OOP) is a programming paradigm centered on creating objects
that encapsulate data (attributes) and behaviors (methods).

Explanation:
OOP focuses on real-world modeling by bundling related data and methods into objects. This
paradigm enables better organization, reusability, and scalability of code.

(b) What are abstract methods? Describe when they are appropriate (2 Marks)

Answer:
Abstract methods are methods declared without a body or implementation. They are typically
used in abstract classes to ensure that all subclasses implement the method.

Explanation:
Abstract methods are ideal when a base class outlines a general blueprint, leaving the specific
behavior to be defined by subclasses. For example, in a Shape class, calculateArea() can be
abstract since each shape (circle, square) has its unique implementation.

(c) Invoice Class Design (15 Marks)

(i) Define the class with required fields (5 Marks)


Answer:

java
Copy code
public class Invoice {
private String partNumber;
private String partDescription;
private int quantity;
private double pricePerItem;
}

Explanation:
The class encapsulates attributes specific to an invoice, such as the part number, description,
quantity, and price.
(ii) Constructor, Getters, Setters, and getInvoiceAmount Method (5 Marks)
Answer:

java
Copy code
public Invoice(String partNumber, String partDescription, int quantity,
double pricePerItem) {
this.partNumber = partNumber;
this.partDescription = partDescription;
this.quantity = Math.max(quantity, 0);
this.pricePerItem = pricePerItem;
}

public String getPartNumber() { return partNumber; }


public void setPartNumber(String partNumber) { this.partNumber = partNumber;
}

public String getPartDescription() { return partDescription; }


public void setPartDescription(String partDescription) { this.partDescription
= partDescription; }

public int getQuantity() { return quantity; }


public void setQuantity(int quantity) { this.quantity = Math.max(quantity,
0); }

public double getPricePerItem() { return pricePerItem; }


public void setPricePerItem(double pricePerItem) { this.pricePerItem =
pricePerItem; }

public double getInvoiceAmount() {


return quantity * pricePerItem;
}

Explanation:
The constructor initializes all fields. Getters and setters allow controlled access to variables.
getInvoiceAmount ensures the total is calculated properly.

(iii) Test Application for Invoice Class (5 Marks)


Answer:

java
Copy code
public class InvoiceTest {
public static void main(String[] args) {
Invoice invoice = new Invoice("1234", "Hammer", 5, 19.99);
System.out.println("Invoice Amount: $" + invoice.getInvoiceAmount());
}
}

Explanation:
This demonstrates the usage of the Invoice class, displaying the invoice amount.
(d) Super Reference and Polymorphism (4 Marks)

Answer:

1. Super Reference: Allows a child class to access its parent class's members directly.
2. Inheritance and Polymorphism: Inheritance enables reusing parent methods.
Polymorphism allows child classes to override behaviors dynamically.

Explanation:
The super keyword avoids ambiguity when parent and child classes share names. Polymorphism
ensures flexibility by allowing multiple behaviors under a common interface.

(e) Principles of OOP (7 Marks)

Answer:
The principles of OOP are encapsulation, inheritance, abstraction, and polymorphism.

Explanation:

 Encapsulation: Restricting access to object internals while exposing only necessary


methods.
 Inheritance: Reusing code by inheriting properties from a base class.
 Abstraction: Hiding implementation details and showing only essential features.
 Polymorphism: Using a single interface to represent different underlying forms.

Question Two: Error and Exception Handling (20 Marks)

(a) Error and Exception Handling in Java (6 Marks)

Answer:
Java offers try-catch blocks, the finally block, the throw keyword, and custom exceptions.

Explanation:

 Try-catch: Wraps code prone to errors.


 Finally: Always executes for cleanup.
 Throw: Used to explicitly throw exceptions.
 Custom Exceptions: Define user-specific exceptions for better debugging.
(b) Program with Try-Catch Block (8 Marks)

Answer:

java
Copy code
import java.util.Scanner;

public class ExceptionDemo {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
try {
System.out.print("Enter first number: ");
int num1 = scanner.nextInt();
System.out.print("Enter second number: ");
int num2 = scanner.nextInt();
System.out.println("Sum: " + (num1 + num2));
} catch (java.util.InputMismatchException e) {
System.out.println("Invalid input. Please enter integers only.");
}
}
}

Explanation:
This handles invalid inputs like letters and prompts the user again.

(c) Interface vs. Derived Class (6 Marks)

Answer:
An interface defines a contract for methods without implementation. A derived class inherits
both properties and methods of a base class.

Example:

java
Copy code
// Interface
public interface Animal {
void sound();
}

// Derived Class
public class Dog extends Animal {
public void sound() {
System.out.println("Woof!");
}
}
Explanation:
Interfaces promote flexibility by ensuring any implementing class provides specific
functionality.

Question Three: SurveyTaker Class (20 Marks)

(a) SurveyTaker Class Components (4 Marks)

Answer:

 Field: numOfYes.
 Constructor: Initializes numOfYes and numOfNo to zero.
 Method: enterResponse.
 Parameter: response.

Explanation:
The field stores data, the constructor sets initial values, and methods operate on the data.

(b) Overloading vs. Overriding (8 Marks)

Answer:

 Overloading: Same method name, different signatures.


 Overriding: Subclass modifies parent class methods.

Example:

java
Copy code
// Overloading
public int add(int a, int b) { return a + b; }
public double add(double a, double b) { return a + b; }

// Overriding
@Override
public void toString() {
return "Overridden toString method";
}

Explanation:
Overloading enhances method usability, while overriding tailors parent class methods to child
class needs.
Question Four & Five: GUI and Applet Programming (20 Marks Each)

(a) GUI with Buttons (8 Marks)

Answer:

java
Copy code
import javax.swing.*;

public class DoctorGUI {


public static void main(String[] args) {
JFrame frame = new JFrame("Doctor Status");
JButton inButton = new JButton("IN");
JButton outButton = new JButton("OUT");

inButton.addActionListener(e -> JOptionPane.showMessageDialog(null,


"DOCTOR IS IN"));
outButton.addActionListener(e -> JOptionPane.showMessageDialog(null,
"DOCTOR IS OUT"));

frame.add(inButton);
frame.add(outButton);
frame.setLayout(new FlowLayout());
frame.setSize(300, 100);
frame.setVisible(true);
}
}

Explanation:
The program dynamically responds to user actions via event listeners.

You might also like