Java实验2 类与对象

实验内容:
(1)编写3个基本类: Triangle, Ladder和Circle,分别用来刻画“三角形”、“梯形”和“圆形”类; 1个主类: Compute,负责计算每个形状的面积或周长。
具体要求:
①Triangle 定义3个变量:边长;和1个求周长的方法。
②Ladder 定义3个变量:上底,下底和高;定义1个求面积的方法。
③Circle 定义1个变量:半径;定义2个方法:求面积、求周长。
④3个基本类都要定义相应的构造方法,对变量进行初始化。
程序设计思路:
定义三个基本类,每个类中定义方法用公式求面积或周长
程序代码:

package java_experiment_second;
import java.util.Scanner;
class Triangle{
	double x,y,z;
	public Triangle(double x,double y,double z) {
		this.x=x;
		this.y=y;
		this.z=z;
	}
	public double Perimeter() {
		return x+y+z;
	}
}
class Ladder{
	double up,down,h;
	public Ladder(double up,double down,double h) {
		this.up=up;
		this.down=down;
		this.h=h;
	}
	public double Area() {
		return (up+down)*h/2;
	}
}
class Circle{
	double r;
	public Circle(double r) {
		this.r=r;
	}
	public double Area() {
		return Math.PI*r*r;
	}
	public double Perimeter() {
		return 2*Math.PI*r;
	}
}
public class Compute {
	double x,y,z,up,down,h,r;
	public static void main(String[] args) {
		Compute c=new Compute();
		Scanner input=new Scanner(System.in);
		System.out.println("请输入三角形边长:");
		c.x=input.nextDouble();
		c.y=input.nextDouble();
		c.z=input.nextDouble();
		Triangle t=new Triangle(c.x,c.y,c.z);
		System.out.println("三角形周长为:");
		System.out.println(t.Perimeter());
		
		System.out.println("请输入梯形的上底、下底和高:");
		c.up=input.nextDouble();
		c.down=input.nextDouble();
		c.h=input.nextDouble();
		Ladder l=new Ladder(c.up,c.down,c.h);
		System.out.println("梯形面积为:");
		System.out.println(l.Area());
		
		System.out.println("请输入圆的半径:");
		c.r=input.nextDouble();
		Circle ci=new Circle(c.r);
		System.out.println("圆的面积为:");
		System.out.println(ci.Area());
		System.out.println("圆的周长为:");
		System.out.println(ci.Perimeter());
	}	
} 

运行截图:
在这里插入图片描述
实验内容:
(2)编写一个账户类Account,它包括:一个名为id的int型账号码属性,一个名为balance的double型的账号余额属性,定义一个类型为java.util.Date的属性dateCreated,用于记录账号的创建日期。同时,定义无参的构造函数,一个名为withDraw的方法从账号提取特定数目的金额,一个名为deposit的方法向账号存入特定数目的金额。请编写测试程序,测试各个方法。
程序设计思路:
定义账户类Accoun,其中的withDraw构造函数,用布尔类型,提取成功为true且余额被减,不成功为false
程序代码:

package java_experiment_second;
import java.util.Scanner;
import java.util.Date;
import java.util.Calendar;
class Account{
	int id;
	double balance;
	Date dateCreated;
	public Account(int id,double balance) {
		this.id=id;
		this.balance=balance;
		this.dateCreated=Calendar.getInstance().getTime();
	}
	public boolean withDrown(double money){
		if(this.balance>=money) {
			this.balance-=money;
			return true;
		}
		else {
			return false;
		}
	}
	public void deposit(double money) {
		this.balance+=money;
	}
	public double getbalance() {
		return this.balance;
	}
}
public class bank {
	int id;
	double balance,money1,money2;
	public static void main(String[] args) {
		bank c=new bank();
		Scanner input=new Scanner(System.in);
		System.out.println("请输入id:");
		c.id=input.nextInt();
		System.out.println("请输入账户余额:");
		c.balance=input.nextDouble();
		Account ac=new Account(c.id,c.balance);
		System.out.println("请输入提款数:");
		c.money1=input.nextDouble();
		boolean flag=ac.withDrown(c.money1);
		if(flag) {
			System.out.println("提款成功,当前账户余额为:"+ac.getbalance());
		}
		else {
			System.out.println("您的余额不足,提款失败,当前账户余额为:"+ac.getbalance());
		}
		System.out.println("请输入存款数:");
		c.money2=input.nextDouble();
		ac.deposit(c.money2);
		System.out.println("存款成功,当前账户余额为:"+ac.getbalance());
		System.out.println("账号更新日期为为:"+ac.dateCreated);
	}
}

运行截图:
在这里插入图片描述
实验内容:
(3)编写一个封装学生的类Student,能够描述学生的“学号”、“ 姓名”、“性别”、“年龄”、“平均成绩”等基本属性,及获取属性、修改属性的方法和打印学生基本信息的print()方法。要求生成两个学生对象,在构造方法中进行初始化,并打印每个学生的基本信息。
程序设计思路:
定义封装学生的类Student,每一个属性都有set和get方法,可对属性进行修改,打印学生基本信息的print()方法直接输出,无返回值。
程序代码:

package java_experiment_second;
class Student{
	private int Id;
	private String Name;
	private String Sex;
	private int Age;
	private double Aver_score;
	public Student(int Id,String Name,String Sex,int Age,double Aver_score) {
		this.setId(Id);
		this.setName(Name);
		this.setSex(Sex);
		this.setAge(Age);
		this.setAver_score(Aver_score);
	}
	public int getId() {
		return Id;
	}
	public void setId(int id) {
		Id = id;
	}
	public String getName() {
		return Name;
	}
	public void setName(String name) {
		Name = name;
	}
	public String getSex() {
		return Sex;
	}
	public void setSex(String sex) {
		Sex = sex;
	}
	public int getAge() {
		return Age;
	}
	public void setAge(int age) {
		Age = age;
	}
	public double getAver_score() {
		return Aver_score;
	}
	public void setAver_score(double aver_score) {
		Aver_score = aver_score;
	}
	public void print() {
		System.out.println("--"+this.getName()+"基本信息:"+"--");
		System.out.println("学号:"+this.getId());
		System.out.println("姓别:"+this.getSex());
		System.out.println("年龄:"+this.getAge());
		System.out.println("平均成绩:"+this.getAver_score());
	}
}
public class information {
	public static void main(String[] args) {
		Student student1=new Student(001,"张三","女",20,89.2);
		student1.print();
		Student student2=new Student(002,"李四","男",19,90.4);
		student2.print();
	}
} 

运行截图:
在这里插入图片描述

  • 9
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值