Home > Back-end >  Beginners Java for help
Beginners Java for help

Time:12-01


Declare a book class, its data members for the title, number (using static variable automatic numbering), price, and have static data members of royalty, total record books of royalty
Using the static variables in the constructor for object number assignment, multiple objects defined in the main method, and the total available
*/

* * not solved: how to make the Numbers in the form of 001002? * *


Define the Book class
` ` ` Java
Public class Book {
String name;
Int id=0;
Static int num=0;
Double price.

Public Book (String name, double price) {
Num +=1;
This. Id +=num;
this.name=name;
This. Price=price;
}
` ` `
Define methods

` ` ` Java
Public void lend () {
If (num & gt; 0 {
System. The out. Println (name + "be borrowed");
Num -=1;
} else {
System. The out. Print (" can not borrow books!" );
}
}
Public void the put () {
System. The out. Println (" book number: "+ id +" title: "+ name +" price: "+ price);
}
Public static void show () {
System. The out. Println (" current "+ num +" books ");
}
` ` `
The main function

` ` ` Java
Public static void main (String [] args) {
The Book book1=new Book (" three body ", 50);
The Book book2=new Book (" Java ", 30);
The Book book3=new Book (" python ", 20);
The Book book4=new Book (" HTML ", 10);
Book1. The put ();
Book2. The put ();
Book2. Lend ();
Book3. The put ();
Book4. The put ();
Show ();
}
` ` `
The results

` ` ` Java
Book number: 1 title: three body price: 50.0
Book number: 2 title: Java price: 30.0
Java is borrowed
Book number: 3 title: python price: 20.0
Book number: title: HTML price: 10.0
Currently there are 3 books
` ` `

  • Related