1、定义一个Book类,该类具有书名、出版社、单价、作者成员属性,对于每个属性提供相应的get和set方法。
2、在Book类中,通过构造方法的重载灵活实现成员变量的初始化。要求:a、无参的构造方法;b、只有书名一个参数的构造方法;c、有书名、出版社两个参数的构造方法;d、有书名...
2、在Book类中,通过构造方法的重载灵活实现成员变量的初始化。要求:a、无参的构造方法;b、只有书名一个参数的构造方法;c、有书名、出版社两个参数的构造方法;d、有书名、出版社、单价三个参数的构造方法;e、有书名、出版社、单价、作者四个参数的构造方法。
3、定义一个方法money,求得购买数量的总金额。要求:没有传入数量(即没有参数)时,直接返回一本书的价格;当传入数量(即有参数)时,金额为单价*数量,且数量只能是int型。通过方法重载来实现。
4、定义一个display方法,显示Book类的成员信息。
5、在测试类中,采用不同的构造方法,实例化5个不同的对象,并分别调用money方法,实现金额的计算。
效果图: 展开
3、定义一个方法money,求得购买数量的总金额。要求:没有传入数量(即没有参数)时,直接返回一本书的价格;当传入数量(即有参数)时,金额为单价*数量,且数量只能是int型。通过方法重载来实现。
4、定义一个display方法,显示Book类的成员信息。
5、在测试类中,采用不同的构造方法,实例化5个不同的对象,并分别调用money方法,实现金额的计算。
效果图: 展开
2个回答
展开全部
import java.util.*;
public class TestBook
{
public static void main(String[] args)
{
Book book1,book2,book3,book4,book5;
book1= new Book();
book2= new Book("一个女人的成长");
book3= new Book("一个女人的成长","人民出版社");
book4= new Book("一个女人的成长","人民出版社",35.5);
book5= new Book("一个女人的成长","人民出版社",35.5,"老兵");
Scanner sc= new Scanner(System.in);
int n;
System.out.println("无参构造方法:");
book1.display();
System.out.println("请输入购买数量:");
n= sc.nextInt();
System.out.println(n+"本书的金额:"+book1.money(n));
System.out.println("具有书名一个参数的构造方法:");
book2.display();
System.out.println("请输入购买数量:");
n= sc.nextInt();
System.out.println(n+"本书的金额:"+book2.money(n));
System.out.println("具有书名、出版社两个参数的构造方法:");
book3.display();
System.out.println("请输入购买数量:");
n= sc.nextInt();
System.out.println(n+"本书的金额:"+book3.money(n));
System.out.println("具有书名、出版社、单价三个参数的构造方法:");
book4.display();
System.out.println("请输入购买数量:");
n= sc.nextInt();
System.out.println(n+"本书的金额:"+book4.money(n));
System.out.println("具有书名、出版社、单价、作者四个参数的构造方法:");
book5.display() ;
System.out.println("请输入购买数量:");
n= sc.nextInt();
System.out.println(n+"本书的金额:"+book5.money(n));
}
}
class Book
{
private String name;
private String house;
private String author;
private double price;
public Book()
{}
public Book(String name)
{
this.name=name;
}
public Book(String name,String house)
{
this.name=name;
this.house=house;
}
public Book(String name,String house,double price)
{
this.name=name;
this.house=house;
this.price=price;
}
public Book(String name,String house,double price,String author)
{
this.name=name;
this.house=house;
this.price=price;
this.author=author;
}
public String getName()
{
return this.name;
}
public void setName(String name)
{
this.name=name;
}
public String getHouse()
{
return this.house;
}
public void setHouse(String house)
{
this.house=house;
}
public String getAuthor()
{
return this.author;
}
public void setAuthor(String author)
{
this.author=author;
}
public double getPrice()
{
return this.price;
}
public void setName(double price)
{
this.price=price;
}
public double money()
{
return this.price;
}
public double money(int amount)
{
return this.price*amount;
}
public void display()
{
System.out.print(name+" ");
System.out.print(house+" ");
System.out.print(price+" ");
System.out.println(author+" ");
}
}
public class TestBook
{
public static void main(String[] args)
{
Book book1,book2,book3,book4,book5;
book1= new Book();
book2= new Book("一个女人的成长");
book3= new Book("一个女人的成长","人民出版社");
book4= new Book("一个女人的成长","人民出版社",35.5);
book5= new Book("一个女人的成长","人民出版社",35.5,"老兵");
Scanner sc= new Scanner(System.in);
int n;
System.out.println("无参构造方法:");
book1.display();
System.out.println("请输入购买数量:");
n= sc.nextInt();
System.out.println(n+"本书的金额:"+book1.money(n));
System.out.println("具有书名一个参数的构造方法:");
book2.display();
System.out.println("请输入购买数量:");
n= sc.nextInt();
System.out.println(n+"本书的金额:"+book2.money(n));
System.out.println("具有书名、出版社两个参数的构造方法:");
book3.display();
System.out.println("请输入购买数量:");
n= sc.nextInt();
System.out.println(n+"本书的金额:"+book3.money(n));
System.out.println("具有书名、出版社、单价三个参数的构造方法:");
book4.display();
System.out.println("请输入购买数量:");
n= sc.nextInt();
System.out.println(n+"本书的金额:"+book4.money(n));
System.out.println("具有书名、出版社、单价、作者四个参数的构造方法:");
book5.display() ;
System.out.println("请输入购买数量:");
n= sc.nextInt();
System.out.println(n+"本书的金额:"+book5.money(n));
}
}
class Book
{
private String name;
private String house;
private String author;
private double price;
public Book()
{}
public Book(String name)
{
this.name=name;
}
public Book(String name,String house)
{
this.name=name;
this.house=house;
}
public Book(String name,String house,double price)
{
this.name=name;
this.house=house;
this.price=price;
}
public Book(String name,String house,double price,String author)
{
this.name=name;
this.house=house;
this.price=price;
this.author=author;
}
public String getName()
{
return this.name;
}
public void setName(String name)
{
this.name=name;
}
public String getHouse()
{
return this.house;
}
public void setHouse(String house)
{
this.house=house;
}
public String getAuthor()
{
return this.author;
}
public void setAuthor(String author)
{
this.author=author;
}
public double getPrice()
{
return this.price;
}
public void setName(double price)
{
this.price=price;
}
public double money()
{
return this.price;
}
public double money(int amount)
{
return this.price*amount;
}
public void display()
{
System.out.print(name+" ");
System.out.print(house+" ");
System.out.print(price+" ");
System.out.println(author+" ");
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
class Book
{
public:
Book():m_name("null"),m_author("null"),
m_publisher("null"),m_price(0)
{
cout<<"无参构造方法..."<<endl;
}
Book(string name):m_name(name),m_author("null"),
m_publisher("null"),m_price(0)
{
cout<<"具有书名一个参数的构造方法..."<<endl;
}
Book(string name,string author)
:m_name(name),m_author(author),
m_publisher("null"),m_price(0)
{
cout<<"具有书名、作者两个参数的构造方法..."<<endl;
}
Book(string name,string author,string publisher)
:m_name(name),m_author(author),
m_publisher(publisher),m_price(0)
{
cout<<"具有书名、作者、出版社三个参数的构造方法..."<<endl;
}
Book(string name,string author,string publisher,double price)
:m_name(name),m_author(author),
m_publisher(publisher),m_price(price)
{
cout<<"具有书名、作者、出版社、单价四个参数的构造方法..."<<endl;
}
~Book(){}
void SetName(string name) {m_name=name;}
string getName() {return m_name;}
void SetPublisher(string publisher) {m_publisher=publisher;}
string getPublisher() {return m_publisher;}
void SetPrice(double price) {m_price=price;}
double getprice() {return m_price;}
void SetAuthor(string author) {m_author=author;}
string getAuthor() {return m_author;}
double money()
{
return m_price;
}
double money(int Count)
{
return m_price*Count;
}
void display()
{
cout<<"《"<<m_name<<"》\t"
<<m_author<<"\t"
<<m_publisher<<"\t"
<<setiosflags(ios::fixed)<<setprecision(1)<<m_price
<<endl<<endl;
}
private:
string m_name;
string m_author;
string m_publisher;
double m_price;
};
int main()
{
Book b1;
b1.display();
Book b2("高级Java网络编程");
b2.display();
Book b3("高级Java网络编程","王五");
b3.display();
Book b4("高级Java网络编程","王五","北京邮电社");
b4.display();
Book b5("高级Java网络编程","王五","北京邮电版社",54);
b5.display();
int n;
cout<<"请输入购买数量:";
cin>>n;
cout<<n<<"本书的金额:"<<b5.money(n);
cin.get();
cin.get();
return 0;
}
#include <iomanip>
#include <string>
using namespace std;
class Book
{
public:
Book():m_name("null"),m_author("null"),
m_publisher("null"),m_price(0)
{
cout<<"无参构造方法..."<<endl;
}
Book(string name):m_name(name),m_author("null"),
m_publisher("null"),m_price(0)
{
cout<<"具有书名一个参数的构造方法..."<<endl;
}
Book(string name,string author)
:m_name(name),m_author(author),
m_publisher("null"),m_price(0)
{
cout<<"具有书名、作者两个参数的构造方法..."<<endl;
}
Book(string name,string author,string publisher)
:m_name(name),m_author(author),
m_publisher(publisher),m_price(0)
{
cout<<"具有书名、作者、出版社三个参数的构造方法..."<<endl;
}
Book(string name,string author,string publisher,double price)
:m_name(name),m_author(author),
m_publisher(publisher),m_price(price)
{
cout<<"具有书名、作者、出版社、单价四个参数的构造方法..."<<endl;
}
~Book(){}
void SetName(string name) {m_name=name;}
string getName() {return m_name;}
void SetPublisher(string publisher) {m_publisher=publisher;}
string getPublisher() {return m_publisher;}
void SetPrice(double price) {m_price=price;}
double getprice() {return m_price;}
void SetAuthor(string author) {m_author=author;}
string getAuthor() {return m_author;}
double money()
{
return m_price;
}
double money(int Count)
{
return m_price*Count;
}
void display()
{
cout<<"《"<<m_name<<"》\t"
<<m_author<<"\t"
<<m_publisher<<"\t"
<<setiosflags(ios::fixed)<<setprecision(1)<<m_price
<<endl<<endl;
}
private:
string m_name;
string m_author;
string m_publisher;
double m_price;
};
int main()
{
Book b1;
b1.display();
Book b2("高级Java网络编程");
b2.display();
Book b3("高级Java网络编程","王五");
b3.display();
Book b4("高级Java网络编程","王五","北京邮电社");
b4.display();
Book b5("高级Java网络编程","王五","北京邮电版社",54);
b5.display();
int n;
cout<<"请输入购买数量:";
cin>>n;
cout<<n<<"本书的金额:"<<b5.money(n);
cin.get();
cin.get();
return 0;
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询