JAVA类与对象相关编程
编写Java程序,模拟简单的计算器。
定义名为Number的类,其中有两个整型数据成员n1和n2,应声明为私有。编写构造方法,赋予n1和n2初始值,再为该类定义加(addition)、减(subtration)、乘(multiplication)、除(division)等公有成员方法,分别对两个成员变量执行加、减、乘、除的运算。
在main方法中创建Number类的对象,调用各个方法,并显示计算结果。
追问:public class Number,public class test 有2个啊 应该只一个公共类把
答案:
public class Number {
private int n1;
private int n2;
public Number(){}
public Number(int n1, int n2) {
this.n1 = n1;
this.n2 = n2;
}
public int getN1() {
return n1;
}
public void setN1(int n1) {
this.n1 = n1;
}
public int getN2() {
return n2;
}
public void setN2(int n2) {
this.n2 = n2;
}
/**
* 加方法
* @return
*/
public static int addition(Number n){
int i=0;
i=n.getN1()+n.getN2();
return i;
}
public static int subtration(Number n){
int i=0;
i=n.getN1()-n.getN2();
return i;
}
public static int multiplication(Number n){
int i=0;
i=n.getN1()*n.getN2();
return i;
}
public static double division(Number n){
double i=0;
if(n.getN1()!=0){
i=n.getN1()/n.getN2();
}else{
System.out.println("被除数不可以为0");
}
return i;
}
}
public class test {
public static void main(String[] args) {
Number n=new Number(10,5);
System.out.println("加法:"+Number.addition(n));
System.out.println("减法:"+Number.subtration(n));
System.out.println("乘法:"+Number.multiplication(n));
System.out.println("除法:"+Number.division(n));
}
}
public class Phone { private String phoneCode;
//电话号码 private String answerPhone;
//接电话
private String call;
//打电话
public Phone() {
super(); }
public Phone(String phoneCode, String answerPhone, String call) {
super();
this.phoneCode = phoneCode;
this.answerPhone = answerPhone;
this.call = call; }
public String getPhoneCode() {
return phoneCode; } public void setPhoneCode(String phoneCode) {
this.phoneCode = phoneCode; } public String getAnswerPhone() {
return answerPhone; } public void setAnswerPhone(String answerPhone) {
this.answerPhone = answerPhone; } public String getCall() {
return call; } public void setCall(String call) {
this.call = call; }
/*
* 输出电话类型
*/ public void phoneType(){
System.out.println("这是电话"); } }
/* * 定义接口 */ public interface Moveable { public void way();
//移动方式 } public class Mobilephone extends Phone implements Moveable {
private String mp3;
//带mp3
public Mobilephone() {
super(); }
public Mobilephone(String phoneCode, String answerPhone, String call,String mp3) {
super(phoneCode,answerPhone,call);
//调用父类代参的构造方法
this.mp3 = mp3; }
public String getMp3() {
return mp3; }
public void setMp3(String mp3) {
this.mp3 = mp3; }
/*
* 实现Moveable 接口中的方法
*/ public void way() {
System.out.println("这是可移动的Mobilephone"); }
/*
* 重写Phone 父类中的方法
*/ public void phoneType(){
System.out.println("这是移动电话"); } }
public class Fixedphone extends Phone { private String leaveWord;
//留言
public Fixedphone() {
super(); }
public Fixedphone(String leaveWord) {
super();
//调用父类无参的构造方法
this.leaveWord = leaveWord; }
/*
* 重写Phone 父类中的方法
*/ public void phoneType(){
System.out.println("这是固定电话"); } }
public class Cordlessphone extends Fixedphone { private String dect;
//数字无绳电话
public Cordlessphone() {
super(); }
public Cordlessphone(String dect) {
super();
this.dect = dect; }
public String getDect() {
return dect; }
public void setDect(String dect) {
this.dect = dect; }
/*
* 重写父类Fixedphone 中的方法
*/ public void phoneType(){
System.out.println("这是无绳电话"); } }
public class TestPhone {
public static void main(String[] args) {
Phone[] phones = new Phone[5];
Phone phone1 = new Phone("4213264","使用phone1接电话","使用phone1打电话");
Phone phone2 = new Phone("3413436","使用phone1接电话","使用phone1打电话");
Phone mobilephone = new Mobilephone("13254742215","使用mobilephone接电话","使用mobilephone打电话","收听mp3");
Phone fixedphone = new Fixedphone("这是固定电话fixedphone");
Phone cordlessphone = new Cordlessphone("这是无绳电话cordlessphone");
phones[0] = phone1;
phones[1] = phone2;
phones[2] = mobilephone;
phones[3] = fixedphone;
phones[4] = cordlessphone;
for(int i=0;i<phones.length; i++){
System.out.println("这是电话的一个变量值:"+phones[i].getCall());
phones[i].phoneType();
} } }
上一个:帮忙做个java程序题目
下一个:java的执行顺序是怎样的