Java Predict
Java Predict
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.
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;
}
Explanation:
The constructor initializes all fields. Getters and setters allow controlled access to variables.
getInvoiceAmount ensures the total is calculated properly.
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.
Answer:
The principles of OOP are encapsulation, inheritance, abstraction, and polymorphism.
Explanation:
Answer:
Java offers try-catch blocks, the finally block, the throw keyword, and custom exceptions.
Explanation:
Answer:
java
Copy code
import java.util.Scanner;
Explanation:
This handles invalid inputs like letters and prompts the user again.
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.
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.
Answer:
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)
Answer:
java
Copy code
import javax.swing.*;
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.