Anonymous

Register for more FREE stuff!

my subscriptions

Constructors

Question 1

[Maximum mark: 4]



Create a Java program that includes a class named Person. This class should have:


  • A constructor that initializes the person's name and age, with values: Jack, 25.
  • A method named printDetails that prints the following statement:
    • "Name: {name}, Age: {age}"

Your program should create an instance of the Person class, call the printDetails method, and display the person's information.

Answers and Explanations

Question 2

[Maximum mark: 5]



Create a Java program that includes a class named Student. This class should have:


  • A constructor that initializes the student's name, ID, and GPA.
  • A method named updateGPA that updates the GPA of the student.
  • A method named printDetails.

Your program should:

  • Create an instance of the Student class with the values: Alice, 12345, 3.5.
  • Update the GPA using the updateGPA method with the value of 3.9.
  • Call the printDetails method to display the student's information.

Ensure that the updateGPA method correctly updates the GPA and that the printDetails method reflects this update.


Your program should print:

  • "Student Name: {name}, ID: {id}, GPA: {gpa}"

Answers and Explanations

Question 3

[Maximum mark: 7]



Create a Java program that includes a class named Account. This class should have:


  • A constructor that initializes the account holder's name and balance.
  • A method named deposit that increases the balance by a specified amount.
  • A method named withdraw that decreases the balance by a specified amount only if the balance is sufficient. If the balance is insufficient, it should print:
    • "Insufficient funds. Transaction failed."
  • A method named printBalance that prints the following statement:
    • "Account Holder: {name}, Balance: ${balance}"

Your program should:

  • Create an instance of the Account class with the following details:
    • Name: "John Doe"
    • Initial Balance: $500

  • Deposit $1000 into the account.
  • Attempt to withdraw $1200 from the account.
  • Attempt to withdraw additional $2000 from the account.
  • Call the printBalance method to display the account holder's information.

Ensure that the withdraw method correctly handles the insufficient funds case, and that the printBalance method accurately reflects the balance after the transactions.

Answers and Explanations

Question 4

[Maximum mark: 7]



Create a BankAccount class in Java that models a bank account. The class will have methods to deposit and withdraw money, check the balance, and display account details. The class will have two private properties: accountNumber and balance.

a) Implement the deposit method. This method will take an amount as a parameter. If the amount is positive, it will add the amount to the balance and print a message indicating the deposit amount. If the amount is not positive, it will print an error message.


b) Then, implement the withdraw method. This method will take an amount as a parameter. If the amount is positive and does not exceed the balance, it will subtract the amount from the balance and print a message indicating the withdrawal amount. If the amount is not positive or exceeds the balance, it will print an error message.


c) Additionally, implement the getBalance method, which will return the current balance. Also, implement the displayAccountDetails method, which will print the account number and current balance to the console.


d) Finally, write the main method to test the BankAccount class. In the main method, create an instance of BankAccount with an initial balance. Perform a series of operations such as depositing money, withdrawing money, and displaying account details to demonstrate the functionality of the class.

Answers and Explanations

Question 5

[Maximum mark: 8]



Create a Java class named Books with the following features:


  • Private Fields: A String field named title and an int field named NumberOfPages.
  • Constructor: A constructor that initializes the title and NumberOfPages fields.
  • Getter Methods: Methods named getTitle and getPages that return the title and number of pages, respectively.

In the main method:

  • Create an array of Books objects with the following books:
    • Title: "BookTitle1", NumberOfPages: 150
    • Title: "BookTitle2", NumberOfPages: 250
    • Title: "BookTitle3", NumberOfPages: 450

  • Iterate through the array and if the book has less than 200 pages print:
    • "Less than 200 pages"
  • If it has more than or equal to 200 pages print:
    • "200 pages or more"

Ensure your program correctly performs these operations and displays the appropriate messages.

Answers and Explanations

Question 6

[Maximum mark: 7]



Consider the following Java class:

                    
public class Publisher {
    private String name, website;
    private int publishes;

    public Publisher (String name, String website, int publishes) {
        this.name = name;
        this.website = website;
        this.publishes = publishes;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    
    public void setWebsite(String website) {
        this.website = website;
    }
    
    public void setPublishes(int publishes) {
        this.publishes = publishes;
    }
    
    public void updatePublishes() {
    
    }
    
    public void printInformation() {

    }
}           
                

a) What is the signature of the setPublishes method?


b) Create a method updatePublishes which will allow the user to update the number of publishes for a given publisher. It should take an argument publishes_new and add this number to the current number of publishes.


c) Create a printInformation method which will print the information about the name, website, and the number of publishes.


Your main method should print:

  • "The name is: {name}"
  • "The website is: {website}"
  • "The number of publishes is: {publishes}"

Answers and Explanations

Question 7

[Maximum mark: 10]



Consider the following Java class:

                    
public class Product {
    private String name;
    private double price;
    private int quantity;

    public Product(String name, double price, int quantity) {
        this.name = name;
        this.price = price;
        this.quantity = quantity;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    
    public void setPrice(double price) {
        this.price = price;
    }
    
    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }
    
    public String getName() {
        return name;
    }
    
    public double getPrice() {
        return price;
    }
    
    public int getQuantity() {
        return quantity;
    }
    
    public double calculateTotalValue() {
    }
    
    public void printDetails() {
    }
}
                    
                

a) Implement a method named calculateTotalValue that calculates the total value of the product by multiplying the price by the quantity. This method should return the total value.


b) Implement a method named printDetails that prints the product details in the following format:

  • "Product: {name}"
  • "Price: ${price}"
  • "Quantity: {quantity}"
  • "Value: {value}"

c) Write a main method to test the Product class. In the main method, perform the following tasks:

  • Create an array of Product objects with the following details:
    • Product 1: "Laptop", $999.99, Quantity: 5
    • Product 2: "Smartphone", $499.99, Quantity: 10
    • Product 3: "Headphones", $199.99, Quantity: 7

  • Use a loop to iterate over the array and:
    • Print the details of each product using the printDetails method.
    • Print the total value of each product using the calculateTotalValue method.
    • Check if the total value is greater than $2000. If it is, print "High-value product"; otherwise, print "Standard-value product".

Your program should correctly perform these operations and print the appropriate messages.

Answers and Explanations