public class Bird { private String bName;//小鸟名 public Bird(String name) { bName = name; } public void fly() {//飞行 System.out.println(bName +"is flying…"); } public void hit() {//撞击 System.out.println(bName +"hits the green pig!"); } public void explode() {//爆炸 System.out.println("Boom!!!"); } }设计 RedBird 类,除构造方法和继承的属性或方法外,仅对外提供 attack 方 法。attack 方法的功能为:先飞行,再撞击。
时间: 2024-03-21 08:38:24 浏览: 84
可以这样设计 RedBird 类:
```
public class RedBird extends Bird {
public RedBird(String name) {
super(name);
}
public void attack() {
fly();
hit();
}
}
```
RedBird 继承自 Bird 类,拥有其所有属性和方法。对外只提供了 attack 方法,实现了先飞行再撞击的功能。在 attack 方法中,调用了父类的 fly 和 hit 方法。
相关问题
class Book{ public String isbn; public String bname; public String publish; public float price; } class BookManage{ private final int BOOK_MAX = 100; private Book []books; private int size; public BookManage(){ books = new Book[BOOK_MAX]; } public void add(Book book){ books[size] = book; size ++; } public int findBookByIsbn(int index, String isbn){ // 补充代码 ...... } public int findBookByBname(int index, String bname){ // 补充代码 ...... } public int findBookByPrice(int index, float price){ // 补充代码 ...... }
你的第一个问题是:
在 BookManage 类中,如何补充代码实现以下三个方法:
1. 根据书的 ISBN 查找书的位置并返回索引值;
2. 根据书名查找书的位置并返回索引值;
3. 根据价格查找书的位置并返回索引值。
答案如下:
1. 根据书的 ISBN 查找书的位置并返回索引值:
```
public int findBookByIsbn(int index, String isbn){
for(int i = 0; i < index; i++){
if(books[i].isbn.equals(isbn)){
return i;
}
}
return -1;
}
```
2. 根据书名查找书的位置并返回索引值:
```
public int findBookByBname(int index, String bname){
for(int i = 0; i < index; i++){
if(books[i].bname.equals(bname)){
return i;
}
}
return -1;
}
```
3. 根据价格查找书的位置并返回索引值:
```
public int findBookByPrice(int index, float price){
for(int i = 0; i < index; i++){
if(books[i].price == price){
return i;
}
}
return -1;
}
```
从键盘依次输入10本图书信息(以顺序表存放),分别编写按书号、书名及价格等信息的查找函数,实现图书信息的查找。 说明:图书信息顺序表数据结构定义。 class Book{ public String isbn; public String bname; public String publish; public float price; } class BookManage{ private final int BOOK_MAX = 100; private Book []books; private int size; public BookManage(){ books = new Book[BOOK_MAX]; } public void add(Book book){ books[size] = book; size ++; } public int findBookByIsbn(int index, String isbn){ // 补充代码 ...... } public int findBookByBname(int index, String bname){ // 补充代码 ...... } public int findBookByPrice(int index, float price){ // 补充代码 ...... } } // findBookByIsbn()方法的代码 // findBookByBname()方法的代码 // findBookByPrice()方法的代码 // main()代码,要求输入多本书信息,再依次执行3种查找方法
以下是完整的代码实现:
```
import java.util.Scanner;
class Book {
public String isbn;
public String bname;
public String publish;
public float price;
}
class BookManage {
private final int BOOK_MAX = 100;
private Book[] books;
private int size;
public BookManage() {
books = new Book[BOOK_MAX];
}
public void add(Book book) {
books[size] = book;
size++;
}
public int findBookByIsbn(int index, String isbn) {
if (index < 0 || index >= size) {
return -1;
}
if (books[index].isbn.equals(isbn)) {
return index;
} else {
return findBookByIsbn(index + 1, isbn);
}
}
public int findBookByBname(int index, String bname) {
if (index < 0 || index >= size) {
return -1;
}
if (books[index].bname.equals(bname)) {
return index;
} else {
return findBookByBname(index + 1, bname);
}
}
public int findBookByPrice(int index, float price) {
if (index < 0 || index >= size) {
return -1;
}
if (books[index].price == price) {
return index;
} else {
return findBookByPrice(index + 1, price);
}
}
}
public class Main {
public static void main(String[] args) {
BookManage bookManage = new BookManage();
Scanner scanner = new Scanner(System.in);
System.out.println("请输入10本书的信息:");
for (int i = 0; i < 10; i++) {
Book book = new Book();
System.out.print("第" + (i + 1) + "本书的书号:");
book.isbn = scanner.next();
System.out.print("第" + (i + 1) + "本书的书名:");
book.bname = scanner.next();
System.out.print("第" + (i + 1) + "本书的出版社:");
book.publish = scanner.next();
System.out.print("第" + (i + 1) + "本书的价格:");
book.price = scanner.nextFloat();
bookManage.add(book);
}
System.out.print("请输入要查找的书的书号:");
String isbn = scanner.next();
int index = bookManage.findBookByIsbn(0, isbn);
if (index == -1) {
System.out.println("未找到该书!");
} else {
System.out.println("该书的信息如下:");
System.out.println("书号:" + bookManage.books[index].isbn);
System.out.println("书名:" + bookManage.books[index].bname);
System.out.println("出版社:" + bookManage.books[index].publish);
System.out.println("价格:" + bookManage.books[index].price);
}
System.out.print("请输入要查找的书的书名:");
String bname = scanner.next();
index = bookManage.findBookByBname(0, bname);
if (index == -1) {
System.out.println("未找到该书!");
} else {
System.out.println("该书的信息如下:");
System.out.println("书号:" + bookManage.books[index].isbn);
System.out.println("书名:" + bookManage.books[index].bname);
System.out.println("出版社:" + bookManage.books[index].publish);
System.out.println("价格:" + bookManage.books[index].price);
}
System.out.print("请输入要查找的书的价格:");
float price = scanner.nextFloat();
index = bookManage.findBookByPrice(0, price);
if (index == -1) {
System.out.println("未找到该书!");
} else {
System.out.println("该书的信息如下:");
System.out.println("书号:" + bookManage.books[index].isbn);
System.out.println("书名:" + bookManage.books[index].bname);
System.out.println("出版社:" + bookManage.books[index].publish);
System.out.println("价格:" + bookManage.books[index].price);
}
}
}
```
运行程序后,按照提示输入10本书的信息,然后分别输入要查找的书的书号、书名和价格,程序会输出相应的书籍信息。
阅读全文