Java query

CSDN Q & A 2022-02-13 09:01:44 阅读数:729

java query

2、 Design a Book Book Class , And write a test program to test Book class .
Definition Book class , Include :
(1) A private int Type class variable bookId, Indicates the book number ,bookid from 1 Start AutoNumber ;
(2) A private String Type instance variable bookName, Indicates the title of the book ;
(4) A private String Type instance variable author, The author ;
(5) A private double Type instance variable price, Indicates the book price ;
(6) Nonparametric construction method ;
(7) have 3 The construction of parameters , The parameter is... Of the book bookName、author、price;
(8) Provide the of all member variables except book number set Method .
(9) Provide all member variables get Method , among bookId Of get Method is a static method .
(10) To write toString() Method returns the information of the book .

Write a test program , It is required in the test procedure :
(1) Create a Book object with a nonparametric construction method book1, And set up ;
(2) Yes 3 The construction method of two parameters creates a Book object book2, It is required to enter this through the keyboard 3 Parameters .
(3) Output object book1 and book2 Information about ;
(4) modify book1 The title of the book 、 The author and the price are “ Object oriented technology ”、“ Zhang San ”,50;
(5) Output the object again book1 and book2 Information about .

The output reference example is shown in the figure .

img




Take the answer :
package Test;import java.util.Scanner;public class Test { public static void main(String[] args) { Book book1 = new Book(); book1.setBookId(1); Scanner scanner = new Scanner(System.in); String str = scanner.nextLine(); String[] split = str.split(" "); Book book2 = new Book(2, split[0], split[1], Double.parseDouble(split[2])); System.out.println(book1); System.out.println(book2); book1.setBookName(" Object oriented technology "); book1.setAuthor(" Zhang San "); book1.setPrice(50); System.out.println(); System.out.println(book1); System.out.println(book2); }}class Book { private int bookId; private String bookName; private String author; private double price; public Book() { super(); // TODO Auto-generated constructor stub } public Book(int bookId, String bookName, String author, double price) { super(); this.bookId = bookId; this.bookName = bookName; this.author = author; this.price = price; } public int getBookId() { return bookId; } public void setBookId(int bookId) { this.bookId = bookId; } public String getBookName() { return bookName; } public void setBookName(String bookName) { this.bookName = bookName; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } @Override public String toString() { return "Book [bookId=" + bookId + ", bookName=" + bookName + ", author=" + author + ", price=" + price + "]"; }}

img


copyright:author[CSDN Q & A],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/02/202202130901424338.html