Question 1 Write a program that computes the number of days that have elapsed si
ID: 3753864 • Letter: Q
Question
Question 1
Write a program that computes the number of days that have elapsed since you were born. Use
the ZonedDateTime class,
Read Chapter 4
Question 2
Write a class BankAccount that implements the Comparable interface type. Order bank accounts
by increasing balance. Supply a test program that sorts an array list of bank accounts
Question 3
Write a program that shows a frame with three buttons labeled “Red”, “Green”, and “Blue”, and
a label containing an icon showing a circle that is initially red. As the user clicks the buttons, the
fill color of the circle should change. When you change the color, you need to invoke the repaint
method on the label. The call to repaint ensures that the paintIcon method is called so that
the icon can be repainted with the new color.
Question 4
Write a program that shows a frame with two buttons labeled “Zoom in”, and “Zoom out”, and a
label containing a car icon. As the user clicks the buttons, the car should get larger or smaller. As
in Question 3, you need to invoke the repaint method on the label to trigger a redisplay of the
image.
Explanation / Answer
As per chegg policy I am doing first 2 questions. Please post last 2 saperatly.
Ques 1
public class DayDifference {
public static void main(String[] args) {
ZonedDateTime now = ZonedDateTime.now(); // US/Pacific
LocalDate date = LocalDate.of(1992, Month.MARCH, 26);
LocalTime time = LocalTime.of(23, 55);
ZoneId zone = ZoneId.of("Australia/Melbourne");
ZonedDateTime born = ZonedDateTime.of(date, time, zone);
long days = born.until(now, ChronoUnit.DAYS);
System.out.println("Number of Dats : "+days);
}
}
Output
Number of Dats : 967
Ques 2
TestSortedBankAccounts.java
class BankAccount implements Comparable<BankAccount> {
/**
* A bank account has a balance that can be changed by deposits and
* withdrawals.
*/
private int accountNumber;
private double balance;
/**
* Constructs a bank account with a zero balance
*
* @param anAccountNumber
* the account number for this account
*/
public BankAccount(int anAccountNumber) {
accountNumber = anAccountNumber;
balance = 0;
}
/**
* Constructs a bank account with a given balance
*
* @param anAccountNumber
* the account number for this account
* @param initialBalance
* the initial balance
*/
public BankAccount(int anAccountNumber, double initialBalance) {
accountNumber = anAccountNumber;
balance = initialBalance;
}
/**
* Gets the account number of this bank account.
*
* @return the account number
*/
public int getAccountNumber() {
return accountNumber;
}
/**
* Deposits money into the bank account.
*
* @param amount
* the amount to deposit
*/
public void deposit(double amount) {
double newBalance = balance + amount;
balance = newBalance;
}
/**
* Withdraws money from the bank account.
*
* @param amount
* the amount to withdraw
*/
public void withdraw(double amount) {
double newBalance = balance - amount;
balance = newBalance;
}
/**
* Gets the current balance of the bank account.
*
* @return the current balance
*/
public double getBalance() {
return balance;
}
public int compareTo(BankAccount temp) {
if (balance < temp.balance)
return -1;
if (balance == temp.balance)
return 0;
return 1;
}
}
public class TestSortedBankAccounts {
public static void main(String[] args) {
// Put bank accounts into a list
ArrayList<BankAccount> list = new ArrayList<BankAccount>();
BankAccount ba1 = new BankAccount(100, 500); // Constructor acctNumber
// and balance
BankAccount ba2 = new BankAccount(200, 10000);
BankAccount ba3 = new BankAccount(300, 400);
BankAccount ba4 = new BankAccount(600, 0);
BankAccount ba5 = new BankAccount(800, 50);
list.add(ba1);
list.add(ba2);
list.add(ba3);
list.add(ba4);
list.add(ba5);
// Call the library sort method
Collections.sort(list);
// Print out the sorted list
for (int i = 0; i < list.size(); i++) {
BankAccount b = list.get(i);
System.out.println(b.getBalance());
}
}
}
Output
0.0
50.0
400.0
500.0
10000.0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.