Java 类和对象 课后习题

目录

1、定义学生类

2、定义矩形类

3、定义日期类

4、定义复数类

5、输入多个学生的学号,姓名,数学,语文,英语成绩,按总分从大到小排序


1、定义学生类

 (1)成员变量:学号(String),姓名(String),专业(String)。 

 (2)构造方法:已知学号,姓名,专业创建学生对象。 

 (3) 编写获取学生信息的方法public String toString(),学生信息格式要求如下: 学号***,姓名***,专业***。 

 (4) 在main方法中测试你写的类: 创建三个学生对象,并输出他们的信息。

import java.util.Scanner;

public class Main {
	public static void main(String args[]) {
		Scanner sc = new Scanner(System.in);
		Student s1 = new Student(sc.next(), sc.next(), sc.next());
		Student s2 = new Student(sc.next(), sc.next(), sc.next());
		Student s3 = new Student(sc.next(), sc.next(), sc.next());
		System.out.println(s1.toString());
		System.out.println(s2.toString());
		System.out.println(s3.toString());
	}
}

class Student {
	String sno, sname, spe; //sno学号 sname姓名 spe专业

	// 构造函数,自动生成,source菜单-generate constructor using fields

	public Student(String sno, String sname, String spe) {
		super();
		this.sno = sno;
		this.sname = sname;
		this.spe = spe;
	}

	@Override
	public String toString() {
		return "学号" + sno + ",姓名" + sname + ",专业" + spe;
	}
}

2、定义矩形类

 (1)成员变量:矩形长度和宽度(整数)。

 (2)构造方法:已知长宽创建矩形对象。 

 (3) 编写计算周长和面积的两个方法。 

 (4)在main方法中测试你写的类: 创建一个矩形对象,并输出它的周长和面积。

import java.util.Scanner;

public class Main {
	public static void main(String args[]) {
		Scanner sc = new Scanner(System.in);
		Rectangle rec1 = new Rectangle(sc.nextInt(), sc.nextInt());
		System.out.println(rec1.c());
		System.out.println(rec1.s());
	}
}

class Rectangle {
	int len, wid; // len长 wid宽

	// 构造函数,自动生成,source菜单-generate constructor using fields

	public Rectangle(int len, int wid) {
		super();
		this.len = len;
		this.wid = wid;
	}

	public int c() { // 矩形周长
		return 2 * (len + wid);
	}

	public int s() { // 矩形面积
		return len * wid;
	}
}

3、定义日期类

 (1)成员变量:年、月、日。

 (2)构造方法1:已知年月日得到日期对象。

 (3)构造方法2:创建默认日期对象(年默认值1900,月默认值1,日默认值1)。

 (4)编写判断年份是否闰年的方法。 

(5)编写计算给定日期是一年中的第几天的方法。 

(6)编写返回日期信息的方法,日期格式:”*年*月*日“。 

(7)在main方法中测试你写的类: 创建一个日期对象,并输出该日期信息及它是一年中的第几天。

import java.util.Scanner;

public class Main {
	public static void main(String args[]) {
		Scanner sc = new Scanner(System.in);
		// 输入 年 月 日
		Date x = new Date(sc.nextInt(), sc.nextInt(), sc.nextInt());
		System.out.println(x);
		System.out.println(x.IsLeapYear());
		System.out.println(x.WhatDays());
	}
}

class Date {
	int year, month, day; // year年 month月 day日

	// 构造函数,自动生成,source菜单-generate constructor using fields

	public Date(int year, int month, int day) {
		super();
		this.year = year;
		this.month = month;
		this.day = day;
	}

	public boolean IsLeapYear() { //判断是否为闰年
		if (year % 3200 == 0) {
			return false;
		} else if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)) {
			return true;
		} else {
			return false;
		}
	}

	public int WhatDays() { // 判断是一年中的第几天
		int days = day;
		int[] m = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
		for (int i = 0; i < month; i++) {
			days += m[i];
		}
		if (IsLeapYear() && month > 2) { // 闰年2月有29天
			days++;
		}
		return days;
	}

	@Override
	public String toString() {
		return year + "年" + month + "月" + day + "日";
	}

}

4、定义复数类

(1)成员变量:实部(Double)和虚部(Double) 

(2)构造方法1:已知实部、虚部得到复数对象。

(3)构造方法2:创建默认复数对象,实部和虚部默认0.0。 

(4)成员方法:编写复数的加、减、乘、除、返回复数信息(实部+虚部*i)方法。
(四种运算可以重载多个方法,比如对加运算,可以两个复数加,可以复数和实数加)

(5)在main方法中测试你写的类: 创建两个复数对象,输出它们的加、减、乘、除的结果信息。

import java.util.Scanner;

public class Main {
	public static void main(String args[]) {
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入复数c1");
		Complex c1 = new Complex(sc.nextDouble(), sc.nextDouble());
		System.out.println(c1);
		System.out.println("请输入复数c2");
		Complex c2 = new Complex(sc.nextDouble(), sc.nextDouble());
		System.out.println(c2);
		Complex c3 = new Complex(0.0, 0.0);
		System.out.println("c1" + "+" + "c2:");
		c1.add(c2);
		System.out.println("c1" + "-" + "c2:");
		c1.sub(c2);
		System.out.println("c1" + "×" + "c2:");
		c1.mul(c2);
		System.out.println("c1" + "÷" + "c2:");
		c1.div(c2);

	}
}

class Complex {
	double realpart, imaginarypart; /* realpart实部 imaginarypart虚部 */

	// 构造函数,自动生成,source菜单-generate constructor using fields

	public Complex(double realpart, double imaginarypart) {
		super();
		this.realpart = realpart;
		this.imaginarypart = imaginarypart;
	}

	public void add(Complex x) {// 加
		realpart = realpart + x.realpart;
		imaginarypart = imaginarypart + x.imaginarypart;
		System.out.println("复数:" + realpart + "+" + imaginarypart + "*i");
	}

	public void sub(Complex x) {// 减
		realpart = realpart - x.realpart;
		imaginarypart = imaginarypart - x.imaginarypart;
		System.out.println("复数:" + realpart + "+" + imaginarypart + "*i");
	}

	public void mul(Complex x) {// 乘
		realpart = (realpart * x.realpart) - (imaginarypart * x.imaginarypart);
		imaginarypart = (imaginarypart * x.realpart)
				+ (realpart * x.imaginarypart);
		System.out.print("复数:");
		System.out.printf("%.1f", realpart);
		if (imaginarypart >= 0) {
			System.out.print("+");
		}
		System.out.printf("%.1f", imaginarypart);
		System.out.println("*i");
	}

	public void div(Complex x) {// 除
		realpart = ((realpart * x.realpart) + (imaginarypart * x.imaginarypart))
				/ (x.realpart * x.realpart + x.imaginarypart * x.imaginarypart);
		imaginarypart = ((imaginarypart * x.realpart) - (realpart * x.imaginarypart))
				/ ((x.realpart * x.realpart + x.imaginarypart * x.imaginarypart));
		System.out.print("复数:");
		System.out.printf("%.1f", realpart);
		if (imaginarypart >= 0) {
			System.out.print("+");
		}
		System.out.printf("%.1f", imaginarypart);
		System.out.println("*i");
	}

	@Override
	public String toString() {
		return "复数:" + realpart + "+" + imaginarypart + "*i";
	}
}
//代码二
public class Main {
    public static void main(String[] args) {
        Complex c1 = new Complex(1, 2);
        Complex c2 = new Complex(3, 4);
        double c3=5;
        System.out.println(c1.add(c2));
        System.out.println(c1.mul(c3));
    }
}
 
class Complex {
    double real, ima;
 
    public Complex(double a, double b) {
        real = a;
        ima = b;
    }
 
    public Complex add(Complex a) {
        return new Complex(this.real + a.real, this.ima + a.ima);
    }
 
    public Complex add(double a) {
        return new Complex(this.real + a, this.ima);
    }
 
    public Complex sub(Complex a) {
        return new Complex(this.real - a.real, this.ima - a.ima);
    }
 
    public Complex sub(double a) {
        return new Complex(this.real - a, this.ima);
    }
 
    public Complex mul(Complex a) {
        return new Complex(this.real * a.real - this.ima * a.ima, this.real * a.ima + this.ima * a.real);
    }
 
    public Complex mul(double a) {
        return new Complex(this.real * a, this.ima * a);
    }
 
    public Complex div(Complex a) {
        double z = a.real * a.real + a.ima * a.ima;
        double x = (this.real * a.real + this.ima * a.ima) / z;
        double y = (this.ima * a.real - this.real * a.ima) / z;
        return new Complex(x, y);
    }
 
    public Complex div(double a) {
 
        return new Complex(this.real / a, this.ima / a);
    }
 
    // 重新改写父类Object中的toString()方法
    public String toString() {
        if (ima >= 0.0)
            return real + "+" + ima + "*i";
        else
            return real + "-" + (-ima) + "*i";
    }
}

5、输入多个学生的学号,姓名,数学,语文,英语成绩,按总分从大到小排序

import java.util.Scanner;

public class Main {
	public static void main(String args[]) {
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入学生人数:");
		int n = sc.nextInt();
		Student[] students = new Student[n];
		for (int i = 0; i < n; i++) {
			students[i] = new Student(sc.next(), sc.next(), sc.nextInt(), sc.nextInt(), sc.nextInt());
		}
		// 选择排序
		for (int i = 0; i < n - 1; i++) {
			int max = i;
			for (int j = i + 1; j < n; j++) {
				if (students[j].sum > students[max].sum) {
					max = j;
				}
			}
			Student t = students[i];
			students[i] = students[max];
			students[max] = t;
		}
		System.out.println("学号" + "\t" + "姓名" + "\t" + "数学" + "\t" + "语文" + "\t" + "英语" + "\t" + "总分");
		for (int i = 0; i < n; i++) {
			System.out.println(students[i].toString());
		}
	}
}

class Student {
	String spe, sname; // 学号、姓名
	int math, chinese, english; // 数学、语文、英语成绩
	int sum;// 总分

	// 构造函数,自动生成,source菜单-generate constructor using fields

	public Student(String spe, String sname, int math, int chinese, int english) {
		super();
		this.spe = spe;
		this.sname = sname;
		this.math = math;
		this.chinese = chinese;
		this.english = english;
		this.sum = math + chinese + english;
	}

	@Override
	public String toString() {
		return spe + "\t" + sname + "\t" + math + "\t" + chinese + "\t"
				+ english + "\t" + sum;
	}
}

  • 3
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Java面向对象程序设计》(第2版)课后答案 《Java面向对象程序设计》(第2版)课后答案全文共39页,当前为第1页。《Java面向对象程序设计》(第2版)课后答案全文共39页,当前为第1页。Java面向对象程序设计 《Java面向对象程序设计》(第2版)课后答案全文共39页,当前为第1页。 《Java面向对象程序设计》(第2版)课后答案全文共39页,当前为第1页。 (编著耿祥义X跃平) 习题1 1.James Gosling 2. (1)使用一个文本编辑器编写源文件。 (2)使用Java编译器(javac.exe)编译Java源程序,得到字节码文件。 (3)使用Java解释器(java.exe)运行Java程序 3.Java的源文件是由若干个书写形式互相独立的类组成的。 应用程序中可以没有public类,若有的话至多可以有一个public类。 4.系统环境path D\jdk\bin; 系统环境classpath D\jdk\jre\lib\rt.jar;.; 5. B 6. Java源文件的扩展名是.javaJava字节码的扩展名是.class。 7. D 8.(1)Speak.java (2)生成两个字节码文件,这些字节码文件的名字Speak.class 和 Xiti8.class (3)java Xiti8 (4)执行java Speak的错误提示 Exception in thread "main" java.lang.NoSuchMethodError: main 执行java xiti8得到的错误提示 Exception in thread "main" java.lang.NoClassDefFoundError: xiti8 (wrong name: Xiti8) 执行java Xiti8.class得到的错误提示 Exception in thread "main" java.lang.NoClassDefFoundError: Xiti8/class 执行java Xiti8得到的输出结果 I'm glad to meet you 9.属于操作题,解答略。 习题2 1. D 2.【代码1】【代码2】错误 //【代码3】更正为 float z=6.89F; 3.float型常量后面必须要有后缀"f"或"F"。 对于double常量,后面可以有后缀"d"或"D",但允许省略该后缀。 4.public class Xiti4{ public static void main (String args[ ]){ char ch1='你',ch2='我',ch3='他'; System.out.println("\""+ch1+"\"的位置:"+(int)ch1); System.out.println("\""+ch2+"\"的位置:"+(int)ch2); System.out.println("\""+ch3+"\"的位置:"+(int)ch3); } } 5.数组名字.length 6.数组名字.length 7.【代码1】A,65 【代码2】-127 《Java面向对象程序设计》(第2版)课后答案全文共39页,当前为第2页。《Java面向对象程序设计》(第2版)课后答案全文共39页,当前为第2页。【代码3】 123456.783,123456.78312 《Java面向对象程序设计》(第2版)课后答案全文共39页,当前为第2页。 《Java面向对象程序设计》(第2版)课后答案全文共39页,当前为第2页。 8. 【代码1】false 【代码2】true 【代码3】false 【代码4】3 【代码5】4.4 【代码6】8.8 习题3 输出110 if-else语句书写的不够规X,复合语句缺少大括号"{}",代码不够清晰。 2.你好好酷!! 3. public class Xiti3_3 { public static void main (String args[ ]){ int startPosition=0,endPosition=0; char cStart='а',cEnd='я'; startPosition=(int)cStart; //cStart做int型转换据运算,并将结果赋值给startPosition endPosition=(int)cEnd ; //cEnd做int型转换运算,并将结果赋值给endPosition System.out.println("俄文字母表:"); for(int i=startPosition;i<=endPosition;i++){ char c='\0'; c=(char)i; //i做char型转换运算,并将结果赋值给c System.out.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

再见以前说再见

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值