Java面向对象开发作业
【摘要】 Java面向对象开发作业
目录
1、设计一个包含多个构造方法的类,并分别用这些构造方法实例化对象
2、编写一个类Calculate1,实现加、减两种运算,然后,编写另一个派生类Calculate2,实现乘、除两种运算
居民类:
4、编写一个类,其中包含一个排序的方法sort(),当传入的是一串整数,就按照从小到大的顺序输出,如果传入的是一个字符串,就将字符串反序输出
5.设计一个立方体类Box,它能计算并输出立方体的体积和表面积
6.编写一个程序,设计一个点类Point,实例化两个点之后,能调用其方法求两个点之间的距离
Demo类:
1、设计一个包含多个构造方法的类,并分别用这些构造方法实例化对象
class Figure {
public String name;
public int age;
public String gender;
Figure(){}
Figure(String name,int age){
this.name = name;
this.age = age;
}
Figure(String name,int age ,String gender){
this.name = name;
this.age = age;
this.gender = gender;
}
}
public class Text01 {
public static void main(String[] args){
Figure one = new Figure("孙悟空",500,"男");//创建对象one
System.out.println("第一个人物是:");
System.out.println(one.name);//使用对象
System.out.println(one.age);
System.out.println(one.gender);
Figure two = new Figure("唐僧",30);//创建对象two
System.out.println("第二个人物是:");
System.out.println(two.name);//使用对象
System.out.println(two.age);
}
}
2、编写一个类Calculate1,实现加、减两种运算,然后,编写另一个派生类Calculate2,实现乘、除两种运算
Calculate1:
public class Calculate1 {
//加法运算
public double sum(double sum1,double sum2){
return sum1+sum2;
}
//减法运算
public double substruction(double sub1,double sub2){
return sub1-sub2;
}
}
派生类Calculate2:
public class Calculate2 extends Calculate1{
//因为继承了Calculate1故加法和减法可以用父类的
//乘法运算
public double multiply(double mul1,double mul2){
return mul1*mul2;
}
//除法运算
public double divide(double div1,double div2){
return div1/div2;
}
}
3、建立三个类:居民、成人、官员。居民包含身份证号、姓名、出生日期,而成人继承自居民,多包含学历、职业两项数据;官员则继承自成人,多包含党派、职务两项数据。要求每个类的字段都以属性的方式对外提供数据输入输出的功能
居民类:
import java.util.Date;
public class Resident {
private String idCard;
private String name;
private Date birth;
public String getIdCard() {
return idCard;
}
public void setIdCard(String idCard) {
this.idCard = idCard;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
}
成人继承自居民:
public class Adult extends Resident {
private String education;
private String vocation;
public String getEducation() {
return education;
}
public void setEducation(String education) {
this.education = education;
}
public String getVocation() {
return vocation;
}
public void setVocation(String vocation) {
this.vocation = vocation;
}
}
官员则继承自成人:
public class Official extends Adult {
private String party;
private String duties;
public String getParty() {
return party;
}
public void setParty(String party) {
this.party = party;
}
public String getDuties() {
return duties;
}
public void setDuties(String duties) {
this.duties = duties;
}
}
4、编写一个类,其中包含一个排序的方法sort(),当传入的是一串整数,就按照从小到大的顺序输出,如果传入的是一个字符串,就将字符串反序输出
import java.util.Arrays;
public class Comment {
public static void main(String[] args) {
String[] objs = { "f", "b", "m", "c", "y" };
int[] obj = { 1, 2, 5, 3, 8 };
objs = sort(objs);//调用方法排序
obj = sort(obj);
//输出已经排序好的字符串
for (String i : objs) {
System.out.print(i + " ");
}
System.out.println();
for(int s : obj) {
System.out.print(s + " ");
}
}
public static int[] sort(int[] nums) {
Arrays.sort(nums);
return nums;
}
// 冒泡排序
public static String[] sort(String[] strs) {
for (int i = 0; i < strs.length; i++) {
for (int j = 0; j < strs.length - i - 1; j++) {
if (strs[j].compareTo(strs[j + 1]) < 1) {
String temp = strs[j];
strs[j] = strs[j + 1];
strs[j + 1] = temp;
}
}
}
return strs;
}
}
5.设计一个立方体类Box,它能计算并输出立方体的体积和表面积
import java.util.Scanner;
public class Box {
double length;
double width;
double height;
// 求立方体体积
double volume(double length, double width, double height) {
return length * width * height;
}
// 求立方体表面积
double area(double length, double width, double height) {
return 2 * (length * width + length * height + width * height);
}
public static void main(String[] args) {
Box box = new Box(); // 实例化一个对象
System.out.println("请输入该立方体的长、宽、高:");
Scanner sc = new Scanner(System.in);
box.length = sc.nextDouble();
box.width = sc.nextDouble();
box.height = sc.nextDouble();
System.out.println("立方体的体积为:" + box.volume(box.length, box.width, box.height)); // 调用方法
System.out.println("立方体的表面积为:" + box.area(box.length, box.width, box.height));
}
}
6.编写一个程序,设计一个点类Point,实例化两个点之后,能调用其方法求两个点之间的距离
点类Point:
public class Point {
private double x ;
private double y ;
public Point(double x, double y) {
this.x = x;
this.y = y;
}
public static void distance(Point p1,Point p2) {
double sum = Math.sqrt(Math.pow(p1.x-p2.x, 2)+Math.pow(p1.y-p2.y, 2));
System.out.println(sum);
}
}
点类Point的测试类:
public class PointDemo {
public static void main(String[] args) {
Point.distance(new Point(0,0), new Point(3,4));
}
}
7、 写一个学生类,包括属性: 学号,班号,姓名,年龄,性别;要求用无参构造,方法获得:学号,班号,姓名,年龄(只能小于100岁,大于1,否则重新输入) ,性别(只能男或者女,否则重新输入),最后在主方法输出你对一个学生对象赋值的信息
public class Student {
static int studentID;
static int classID;
static String name;
static int age;
static char sex;
public Student() {
}
public int getStudentID() {
return studentID;
}
public void setStudentID(int studentId) {
this.studentID = studentId;
}
public int getClassID() {
return classID;
}
public void setClassID(int classID) {
this.classID = classID;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
if (age > 1 && age < 100)
this.age = age;
else
throw new RuntimeException("输入年龄:大于1,小于100");
}
public char getSex() {
return sex;
}
public void setSex(char sex) {
if (sex == '男' || sex == '女')
this.sex = sex;
else
throw new RuntimeException("只能男或者女");
}
public static void main(String[] args) {
Student aa = new Student();
aa.setStudentID(54321);
aa.setClassID(121);
aa.setName("风清扬");
aa.setAge(20);
aa.setSex('男');
System.out.println("学号:" + studentID + "\n" + "班号:" + classID);
System.out.print("姓名:" + name + "\n" + "年龄:" + age + "\n" + "性别:" + sex);
}
8、设计一个BankAccount类,实现银行某账号的资金往来账目管理,包括建账号、存入、取出等。BankAccount类包括,账号(BankAccountId)、开户日期Date(日期),Money(金额),Rest(余额)。另有一个构造方法和三个成员方法Bankin()(处理存入账),Bankout()处理取出账)和和一个负责生成账号的自动增长的方法
class BankAccount {
String Date;
double Money;
String BankAccountId;
public BankAccount() {
BankAccountId = "";
Money = 0.0;
}
public BankAccount(String ID, String date, double balance) {
Date = date;
BankAccountId = ID;
Money = balance;
}
public String getBankAccountId() {
return BankAccountId;
}
public void getBankAccountId(String ID) {
BankAccountId = ID;
}
public double getBalance() {
return Money;
}
public void printAccountMsg() {
System.out.print("账户名:" + BankAccountId + " 开户日期 " + Date + " 当前余额" + Money + "元\n");
}
public void Bankin(double money) {
System.out.print("此次存入:" + money + "元 ");
Money += money;
}
public void Bankout(double money) {
System.out.print("此次取出:" + money + "元 ");
if (money <= Money)
Money -= money;
else
System.out.print("账户余额不足,操作失败!");
}
}
public class Test {
public static void main(String[] args) {
BankAccount user = new BankAccount("user", "2020-03-24", 150000);
user.printAccountMsg();
user.Bankin(15000);
System.out.println();
user.printAccountMsg();
user.Bankout(288888);
System.out.println();
user.printAccountMsg();
}
}
9、编写一个程序,已有若干学生数据,包括学号、姓名、成绩,要求输出这些学生数据并计算平均分。思路:设计一个学生类Stud,除了包括no(学号)、name(姓名)、和deg(成绩)数据成员外。有两个静态变量sum和num,分别存放总分和人数,另有一个构造方法、一个普通成员方法disp()和一个静态成员方法avg(),它用于计算平均分
public class Stud {
int no;
String name;
double deg;
static double sum = 0;
static int num = 0;
public Stud(int no, String name, double deg) {
this.no = no;
this.name = name;
this.deg = deg;
disp();
sum += deg;
num++;
}
public void disp() {
System.out.println("学号:" + this.no + "\t姓名:" + this.name + "\t成绩:" + this.deg);
}
public static void avg() {
System.out.println("平均分:" + sum / num);
}
public static void main(String[] args) {
Stud[] studs = new Stud[5];
studs[0] = new Stud(2022001, "张三", 88);
studs[1] = new Stud(2022002, "李四", 77);
studs[2] = new Stud(2022003, "王五", 68);
studs[3] = new Stud(2022004, "杨六", 87);
studs[4] = new Stud(2022005, "刘七", 95);
Stud.avg();
}
}
10.编写一个程序,统计学生成绩,其功能包括输入学生的姓名和成绩,按成绩从高到低排列打印输出,对前70%的学生定为合格(PASS),而后30%的学生定为不合格(FAIL)。思路: 设计一个类student,包含学生的姓名和成绩等数据。设计一个类Compute: sort()、disp(),它们分别用于按成绩排序和输出数据
Compute类:
public class Compute {
public void sort(Student [] students){
for(int i = 0;i < students.length;i++){
for(int j = i;j < students.length;j++){
if(students[i].deg < students[j].deg){
Student temp = students[i];
students[i] = students[j];
students[j] = temp;
}
}
}
}
public void disp(Student[] students){
sort(students);
for(int i = 0;i < students.length;i++){
if(i <= students.length*0.7-1){
System.out.println("姓名:"+students[i].name+" 成绩:"+students[i].deg+" PASS");
}else {
System.out.println("姓名:"+students[i].name+" 成绩:"+students[i].deg+" FAIL");
}
}
}
}
Student类:
public class Student {
String name;
double deg;
public Student(String name, double deg) {
this.name = name;
this.deg = deg;
}
}
Demo类:
public class Demo {
public static void main(String[] args) {
Student[] s = new Student[5];
s[0] = new Student("张三",79);
s[1] = new Student("李四",88);
s[2] = new Student("王五",56);
s[3] = new Student("杨六",75);
s[4] = new Student("刘七",65);
Compute compute = new Compute();
compute.disp(s);
}
}
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)