0% found this document useful (0 votes)
5 views16 pages

Advanced Java

The document provides code examples for creating a simple loan calculator using Java Swing and JavaFX. It includes a JFrame setup for a graphical interface, a Loan class with methods to calculate monthly and total payments, and event handling for user interactions. Additionally, it covers the usage of the 'this' keyword in Java and demonstrates how to implement a listener method for processing loan calculations.

Uploaded by

rolandtamakole
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views16 pages

Advanced Java

The document provides code examples for creating a simple loan calculator using Java Swing and JavaFX. It includes a JFrame setup for a graphical interface, a Loan class with methods to calculate monthly and total payments, and event handling for user interactions. Additionally, it covers the usage of the 'this' keyword in Java and demonstrates how to implement a listener method for processing loan calculations.

Uploaded by

rolandtamakole
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

And event-driven programming:

Question: Implement a listener method that can process and generate a result similar to fig. 2

Past Questions
Saturday, August 17, 2024
10:07 AM

CICS 312 PAST QUESTION


1. Answers

2. ANSWER
import [Link].*;

public class ExamSwing {


// Constructor
public ExamSwing() {
// Create a JFrame object with the title "Exam Pressure"
JFrame frmExam = new JFrame("Exam Pressure");

// Prevent the user from maximizing the frame


[Link](false);
// Set the width of frmExam to 300 pixels and its height to 200 pixels
[Link](300, 200);

// Indicate that when the user clicks on the close button, the frame should exit
[Link](JFrame.EXIT_ON_CLOSE);

// Create a JLabel object with the text "Exam Fever"


JLabel lblExam = new JLabel("Exam Fever");

// Set the position and size of the lblExam label


[Link](150, 50, 100, 20);

// Add lblExam to frmExam


[Link](lblExam);

// Set the layout to null to use absolute positioning


[Link](null);

// Set the visibility property of frmExam to true


[Link](true);
}

public static void main(String[] args) {


// Create an instance of ExamSwing
new ExamSwing();
}
}

SECTION B
1. Answer:
class thisClass {
double studID;
String name;
double fee;

thisClass(int studID, String name, double fee) {


[Link] = studID;
[Link] = name;
[Link] = fee;
}
}

public class testThis {


public static void main(String[] args) {
// Create two objects of thisClass
thisClass s1 = new thisClass(101, "Alice", 1500.75);
thisClass s2 = new thisClass(102, "Bob", 1800.50);
// Print the details of the two objects
[Link]("Student 1: ID = " + [Link] + ", Name = " + [Link] + ", Fee = " + [Link]);
[Link]("Student 2: ID = " + [Link] + ", Name = " + [Link] + ", Fee = " + [Link]);
}
}

2. The "this" keyword is used these ways:


a. Referencing instance variables
b. Calling another constructor in the same class
c. Passing the Current Object as a Parameter
1. E
2. D
3. A
4. D
5. B
6. B
7. B
8. A
9. C
10. F
11. B
12. E
13. B
14. D
15. A
16. B
17. A
18. B
19. C
20. A
21. A
22. B
23. C
24. B
25. E
26. D
27. C
28. C
29. A
30. A

(1i) Loan
-----------------------------------
- annualInterestRate: double
- numberOfYears: int
- loanAmount: double
- loanDate: [Link]
-----------------------------------
+ Loan()
+ Loan(annualInterestRate: double, numberOfYears: int, loanAmount: double)
+ getAnnualInterestRate(): double
+ getNumberOfYears(): int
+ getLoanAmount(): double
+ getLoanDate(): [Link]
+ setAnnualInterestRate(annualInterestRate: double): void
+ setNumberOfYears(numberOfYears: int): void
+ setLoanAmount(loanAmount: double): void
+ getMonthlyPayment(): double
+ getTotalPayment(): double

ANSWER
import [Link];

public class Loan {


private double annualInterestRate;
private int numberOfYears;
private double loanAmount;
private [Link] loanDate;

// Default constructor
public Loan() {
this(2.5, 1, 1000); // Default values
}

// Constructor with specified annualInterestRate, numberOfYears, and loanAmount


public Loan(double annualInterestRate, int numberOfYears, double loanAmount) {
[Link] = annualInterestRate;
[Link] = numberOfYears;
[Link] = loanAmount;
[Link] = new [Link]();
}
// Getters
public double getAnnualInterestRate() {
return annualInterestRate;
}

public int getNumberOfYears() {


return numberOfYears;
}

public double getLoanAmount() {


return loanAmount;
}

public [Link] getLoanDate() {


return loanDate;
}

// Setters
public void setAnnualInterestRate(double annualInterestRate) {
[Link] = annualInterestRate;
}

public void setNumberOfYears(int numberOfYears) {


[Link] = numberOfYears;
}

public void setLoanAmount(double loanAmount) {


[Link] = loanAmount;
}

// Calculate the monthly payment


public double getMonthlyPayment() {
double monthlyInterestRate = annualInterestRate / 1200;
double monthlyPayment = loanAmount * monthlyInterestRate /
(1 - (1 / [Link](1 + monthlyInterestRate, numberOfYears * 12)));
return monthlyPayment;
}

// Calculate the total payment


public double getTotalPayment() {
return getMonthlyPayment() * numberOfYears * 12;
}
// Creating object
public static void main(String[] args) {

Loan loan1 = new Loan(2.5, 5, 10000);


[Link]("Monthly Payment: " + [Link]());
[Link]("Total Payment: " + [Link]());
}
}

1(ii)
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

public class LoanCalculatorFX extends Application {

@Override
public void start(Stage primaryStage) {
// Create labels and text fields
Label[] labels = {
new Label("Annual Interest Rate:"),
new Label("Number of Years:"),
new Label("Loan Amount:"),
new Label("Monthly Payment:"),
new Label("Total Payment:")
};
TextField[] textFields = {
new TextField(), new TextField(), new TextField(),
new TextField(), new TextField()
};
textFields[3].setEditable(false); // Monthly Payment (read-only)
textFields[4].setEditable(false); // Total Payment (read-only)

// Buttons
Button btnCalculate = new Button("Calculate");
Button btnClear = new Button("Clear");

// Layout
GridPane gridPane = new GridPane();
[Link]([Link]);
[Link](new Insets(10));
[Link](10);
[Link](10);

// Add components to grid


for (int i = 0; i < [Link]; i++) {
[Link](labels[i], 0, i);
[Link](textFields[i], 1, i);
}
[Link](btnClear, 0, 5);
[Link](btnCalculate, 1, 5);

// Event Handling
[Link](e -> {
double annualInterestRate = [Link](textFields[0].getText());
int numberOfYears = [Link](textFields[1].getText());
double loanAmount = [Link](textFields[2].getText());

double monthlyInterestRate = annualInterestRate / 1200;


double monthlyPayment = loanAmount * monthlyInterestRate /
(1 - 1 / [Link](1 + monthlyInterestRate, numberOfYears * 12));
double totalPayment = monthlyPayment * numberOfYears * 12;

textFields[3].setText([Link]("ghs %.2f", monthlyPayment));


textFields[4].setText([Link]("ghs %.2f", totalPayment));
});

[Link](e -> {
for (TextField tf : textFields) {
[Link]();
}
});

// Scene and Stage setup


Scene scene = new Scene(gridPane, 300, 250);
[Link]("Loan Calculator");
[Link](scene);
[Link]();
}

public static void main(String[] args) {


launch(args);
}
}

1(iii)

// Inside the start method, after creating the GUI components:

[Link](event -> {
try {
// Get input values from text fields
double annualInterestRate = [Link]([Link]());
int numberOfYears = [Link]([Link]());
double loanAmount = [Link]([Link]());

// Create a Loan object and calculate payments


Loan loan = new Loan(annualInterestRate, numberOfYears, loanAmount);
double monthlyPayment = [Link]();
double totalPayment = [Link]();

// Update output fields


[Link]([Link]("%.2f", monthlyPayment));
[Link]([Link]("%.2f", totalPayment));
} catch (NumberFormatException e) {
// Handle invalid input
[Link]("Invalid input");
}
});

You might also like