目录
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;
}
}