Java面对对象编程实验
- 编写一个Java Application程序,文件名为 长方体.java。
- 编写类
- 代码:
public class 长方体 {
double length,width,height,S,L;//成员变量
public 长方体(){
length=0;
width=0;
height=0;
}
public 长方体(double c){
length=c;
width=c;
height=c;
}
public 长方体(double c, double k,double g){
length=c;
width=k;
height=g;
}
//三种构造方式
public void setBox(double c, double k, double g){
length=c;
width=k;
height=g;
}
//设置长方体参数
public double getArea(){
S = (length*width+length*height+width*height)*2;
return S;
}
//计算面积
public double getL(){
L = length*width*height;
return S;
}
//计算体积
}
(2)实验运行构造方法
①代码:
public static void main(String[] args) {
长方体 Box1 = new 长方体();
长方体 Box2 = new 长方体(2,3);
长方体 Box3 = new 长方体(1,2,3);
System.out.println("创造成功!");
②运行结果

(3)实验类方法
①代码:
public static void main(String[] args) {
长方体 Box1 = new 长方体();
Box1.setBox(1, 2, 3);
System.out.println("长为"+Box1.length+"宽为"+Box1.width+"高为"+Box1.height);
System.out.println("面积为"+Box1.getArea()+"体积为"+Box1.getL());
}
②运行结果

二、编写复数类
①构造及方法代码
public class Complex {
double real,image,mo;
public Complex(double real, double image) {
this.real=real;this.image=image;//实部和虚部成员变量
}
public Complex add(Complex fushu){
Complex sum=new Complex(this.real+fushu.real,this.image+fushu.image);
return sum;
}//复数加法
public Complex sub(Complex fushu){
Complex dif=new Complex(this.real-fushu.real,this.image-fushu.image);
return dif;
}//复数减法
public double mo(){
this.mo=Math.sqrt(this.real*this.real+this.image*this.image);
return mo;
}//复数求模
public void print(){
if(this.image>0) {
System.out.println(this.real+"+"+this.image+"i");}
else if(this.image<0) {
System.out.println(this.real+""+this.image+"i");}
else {
System.out.println(this.real);}
}
//格式输出
public static void main(String[] args) {
Complex f1=new Complex(3,2);
Complex f2=new Complex(-1,3);
f1.add(f2).print();
f1.sub(f2).print();
System.out.println(f1.mo());
}
}
②试运行代码
public static void main(String[] args) {
Complex f1=new Complex(3,2);
Complex f2=new Complex(-1,3);
f1.add(f2).print();
f1.sub(f2).print();
System.out.println(f1.mo());
}
- 运行结果

三、 设计银行账户类
①代码
import java.util.Date;
import java.util.Scanner;
public class Account {
int no;
String name;
Date time=new Date();
Double balance=0.00;
//成员变量
public Account(String name){
this.name=name;
}
public Account(int no, String name){
this.no=no;
this.name=name;
}
public Account(int no, String name,double balance){
this.no=no;
this.name=name;
this.balance=balance;
}
//构造方法
public void save(Double x){
this.balance=this.balance+x;
}//存钱
public void draw(Double x){
this.balance=this.balance-x;
}//取钱
public void query(){
System.out.println("您的账户余额为:"+this.balance);
}//查询余额
public void calinterest(){
System.out.println("银行存款年利率为:");
Scanner sc=new Scanner(System.in);
Double rate = sc.nextDouble();
sc.close();
System.out.println("一年后您的存款利息为:"+this.balance*rate);
System.out.println("一年后您的本息和为:"+this.balance*(1+rate));
}//计算利率
//成员方法
public static void main(String[] args) {
Account ming=new Account(10010,"xiaoming",1000);
ming.save(300.00);ming.draw(20.50);
ming.query();
ming.calinterest();
//实验方法
}
}
- 成果检验

四、编写石头剪刀布游戏
①代码
import java.util.Scanner;
class Human{
String cai;
public Human(){
System.out.println("石头剪刀布!请问你要出什么呢?");
Scanner sc=new Scanner(System.in);
this.cai = sc.next();
sc.close();
}
public String result() {return this.cai;}
}
class Computer{
String cai;
public Computer(){
double n=Math.random()*3;
if (n<=1){this.cai="石头"; }
else if (n<=2){this.cai="剪刀"; }
else{this.cai="布";}
System.out.println("电脑出了"+this.cai);
}
public String result() {return this.cai;}
}
public class Game {
String rehuman,recomp;
public Game(Human a, Computer b){
this.rehuman=a.result();
this.recomp=b.result();
}
public void judge(){
if (rehuman.equals(recomp)){
System.out.println("打成了平局= =");
}
else if(rehuman.equals("石头")){
if (recomp.equals("剪刀")){System.out.println("恭喜你赢了>_<");}
else{System.out.println("可惜呀,你输了QAQ");}
}
else if(rehuman.equals("剪刀")){
if (recomp.equals("布")){System.out.println("恭喜你赢了>_<");}
else{System.out.println("可惜呀,你输了QAQ");}
}
else {
if (recomp.equals("石头")){System.out.println("恭喜你赢了>_<");}
else{System.out.println("可惜呀,你输了QAQ");}
}
}
public static void main(String[] args) {
Human a = new Human();
Computer b = new Computer();
Game g1 = new Game(a,b);
g1.judge();
}
}
②运行结果

五、编写一个掷骰子数字游戏
①代码(由于throw是关键词所以代码中掷骰子的方法名改成了roll)
import javax.swing.JOptionPane;
public class Game {
int v;
int num;
public Game(){
this.v=1;
}//初始化
public void roll() {
this.v=(int)(Math.random()*6+1);
System.out.println("已经随机掷出了一枚骰子");
}//随机扔骰子存到v
public void guess() {
String shu=JOptionPane.showInputDialog("猜一个数字:");
this.num=Integer.parseInt(shu);
}//弹出弹窗输入猜想值
public static void main(String[] args) {
Game g1=new Game();
g1.roll();
g1.guess();
while(g1.v!=g1.num) {
if(g1.v<g1.num) {System.out.println("猜的有点大哦,再试试吧");}
else {System.out.println("猜的有点小哦,再试试吧");}
g1.guess();
}
System.out.println("恭喜你猜对了!掷出的是"+g1.v+"点!");
}
}
②运行结果

9825

被折叠的 条评论
为什么被折叠?



