Name: John, Age: 25
Close
public class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public void printDetails() {
System.out.println("Name: " + name + ", Age: " + age);
}
public static void main(String[] args) {
Person person = new Person("John", 25);
person.printDetails();
}
}
Close
Student Name: Alice, ID: 12345, GPA: 3.9
Close
public class Student {
private String name;
private int id;
private double gpa;
public Student(String name, int id, double gpa) {
this.name = name;
this.id = id;
this.gpa = gpa;
}
public void updateGPA(double newGPA) {
this.gpa = newGPA;
}
public void printDetails() {
System.out.println("Student Name: " + name + ", ID: " + id + ", GPA: " + gpa);
}
public static void main(String[] args) {
// Create a new student object
Student student = new Student("Alice", 12345, 3.5);
// Update the GPA
student.updateGPA(3.9);
// Print student details
student.printDetails();
}
}
Close
public class Account {
private String name;
private double balance;
public Account(String name, double balance) {
this.name = name;
this.balance = balance;
}
public void deposit(double amount) {
balance += amount;
}
public void withdraw(double amount) {
if (balance >= amount) {
balance -= amount;
} else {
System.out.println("Insufficient funds. Transaction failed.");
}
}
public void printBalance() {
System.out.println("Account Holder: " + name + ", Balance: $" + balance);
}
public static void main(String[] args) {
// Create an account object
Account account = new Account("John Doe", 500);
// Deposit $1000
account.deposit(1000);
// Attempt to withdraw $1200
account.withdraw(1200);
// Attempt to withdraw $2000
account.withdraw(2000);
// Print account balance
account.printBalance();
}
}
Close
public class BankAccount {
private String accountNumber;
private double balance;
public BankAccount(String accountNumber, double initialBalance) {
this.accountNumber = accountNumber;
this.balance = initialBalance;
}
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
System.out.println("Deposit: $" + amount);
} else {
System.out.println("Error: Deposit amount must be positive.");
}
}
public void withdraw(double amount) {
if (amount > 0 && amount <= balance) {
balance -= amount;
System.out.println("Withdrawal: $" + amount);
} else {
System.out.println("Error: Insufficient balance or invalid amount.");
}
}
public double getBalance() {
return balance;
}
public void displayAccountDetails() {
System.out.println("Account Number: " + accountNumber + ", Balance: $" + balance);
}
public static void main(String[] args) {
// Create a bank account with an initial balance of $500
BankAccount account = new BankAccount("123456789", 500);
// Deposit $500
account.deposit(500);
// Withdraw $200
account.withdraw(200);
// Display account details
account.displayAccountDetails();
}
}
Close
BookTitle1: Less than 200 pages
BookTitle2: More than 200 pages
BookTitle3: More than 200 pages
Close
public class Books {
private String title;
private int numberOfPages;
public Books(String title, int numberOfPages) {
this.title = title;
this.numberOfPages = numberOfPages;
}
public String getTitle() {
return title;
}
public int getPages() {
return numberOfPages;
}
public static void main(String[] args) {
// Create and initialize an array of Books objects in one line
Books[] books = {
new Books("BookTitle1", 150),
new Books("BookTitle2", 250),
new Books("BookTitle3", 450)
};
// Iterate through the array and print the appropriate messages
for (Books book : books) {
if (book.getPages() < 200) {
System.out.println(book.getTitle() + ": Less than 200 pages");
} else {
System.out.println(book.getTitle() + ": More than 200 pages");
}
}
}
}
Close
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(int publishes_new) {
this.publishes += publishes_new;
}
public void printInformation() {
System.out.println("The name is: " + name);
System.out.println("The website is: " + website);
System.out.println("The number of publishes is: " + publishes);
}
public static void main(String[] args) {
// Part A: public void setPublishes(int publishes)
Publisher publisher = new Publisher("Penguin Books", "www.penguin.com", 100);
// Update the number of publishes
publisher.updatePublishes(50);
// Print the publisher's information
publisher.printInformation();
}
}
Close
Product: Laptop
Price: $999.99
Quantity: 5
Value: $4999.95
High-value product
Product: Smartphone
Price: $499.99
Quantity: 10
Value: $4999.90
High-value product
Product: Headphones
Price: $199.99
Quantity: 7
Value: $1399.93
Standard-value product
Close
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() {
return price * quantity;
}
public void printDetails() {
double value = calculateTotalValue();
System.out.println("Product: " + name);
System.out.println("Price: $" + price);
System.out.println("Quantity: " + quantity);
System.out.println("Value: $" + value);
}
public static void main(String[] args) {
Product[] products = {
new Product("Laptop", 999.99, 5),
new Product("Smartphone", 499.99, 10),
new Product("Headphones", 199.99, 7)
};
for (Product product : products) {
product.printDetails();
double totalValue = product.calculateTotalValue();
System.out.println("Total Value: $" + totalValue);
if (totalValue > 2000) {
System.out.println("High-value product");
} else {
System.out.println("Standard-value product");
}
System.out.println();
}
}
}
Close
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() {
}
}
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() {
}
}