make a program in java (I use Intellij IDEA) following this scheme:
Author
-name: String //No default values for the variables
-email: String
-gender: char //char of 'm' or 'f'
+Author (name: String, email: String, gender: char)
+getName(): String
+getEmail(): String
+setEmail (email: String):void
+getGender(): char
+toString(): String //"Author [name=?, email=?,gender=?]"
Book
-name: String
-author: Author
-price:double
-qty:int = 0
+Book (name: String, author: Author, price:double)
+Book (name: String, author: Author, price:double,qty:int)
+getName(): String
+getAuthor(): Author
+getPrice(): double
+setPrice(price:double):void +getQty(): int
+setQty (qty: int):void
+toString(): String //"Book[name=?, Author [name=?, email=?,gender=?],price=?,qty=?]" You need to reuse Author's toString().
Author //connected to Book
-name: String
-email: String
-gender: char
Assistant
Below is an example of how you can implement the Author and Book classes in Java following the provided scheme. Please note that this example assumes you have a basic understanding of Java classes and object-oriented programming concepts.
You can place each class in its own file (Author.java and Book.java), or you can place them both in a single file if you prefer but remember to have only one public class per file, the one that matches the filename.
Author.java:
java
public class Author {
private String name;
private String email;
private char gender; // 'm' or 'f'
public Author(String name, String email, char gender) {
this.name = name;
this.email = email;
this.gender = gender;
}
public String getName() {
return name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public char getGender() {
return gender;
}
@Override
public String toString() {
return "Author[name=" + name + ", email=" + email + ", gender=" + gender + "]";
}
}
Book.java:
java
public class Book {
private String name;
private Author author;
private double price;
private int qty;
public Book(String name, Author author, double price) {
this.name = name;
this.author = author;
this.price = price;
}
public Book(String name, Author author, double price, int qty) {
this.name = name;
this.author = author;
this.price = price;
this.qty = qty;
}
public String getName() {
return name;
}
public Author getAuthor() {
return author;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getQty() {
return qty;
}
public void setQty(int qty) {
this.qty = qty;
}
@Override
public String toString() {
return "Book[name=" + name + ", " + author.toString() + ", price=" + price + ", qty=" + qty + "]";
}
}
To use these classes, you can create a test class with a main method.
Main.java:
java
public class Main {
public static void main(String[] args) {
// Create an instance of Author
Author author = new Author("Jane Doe", "jane.doe@example.com", 'f');
// Create a Book instance
Book book = new Book("Java for Beginners", author, 29.95, 100);
// Print information about the book
System.out.println(book.toString());
// Update author's email and book's price and quantity
author.setEmail("jane.d@example.com");
book.setPrice(34.99);
book.setQty(120);
// Print updated information about the book
System.out.println(book.toString());
}
}
In IntelliJ IDEA, you would typically create a project and place each class in its own .java file within the src directory. Make sure your directory structure and package declarations (if used) are correct. Run Main.java to test your classes.