6-5 奖学金资格审查系统
class Undergraduate extends Student{
Undergraduate(String id,double ca)
{
super(id,ca);
}
public boolean isReq()
{
return super.getCA()>=80;
}
}
class Graduate extends Student{
boolean supervisor;
Graduate(String id,double ca,boolean supervisor)
{
super(id,ca);
this.supervisor = supervisor;
}
public boolean isReq()
{
return super.getCA()>=85&&supervisor;
}
}
6-6 求圆面积自定义异常类
class Circle{
double r;
public Circle(double r){
this.r=r;
}
double area() throws CircleException {
if(this.r<0){
throw new CircleException(this.r);
}
return 3.14*r*r;
}
}
class CircleException extends Exception{
double r;
public CircleException(double r)
{
super();
this.r=r;
}
void print(){
System.out.println("圆半径为"+this.r+"不合理");
}
}
6-7 创建一个直角三角形类实现IShape接口
class RTriangle implements IShape{
private double a,b;
public RTriangle(double a,double b)
{
this.a = a;
this.b= b;
}
//面积
public double getArea()
{
return 0.5*a*b;
}
//周长
public double getPerimeter()
{
return a+b+Math.sqrt(a*a+b*b);
}
}
6-8 为中国加油,为名族争气,华为手机设计
import java.util.*;
class Phone{
private String brand;
private String model;
private String os;
private String chip;
private int price;
private boolean isOn;
//初始化
public Phone(String brand, String model, String os, String chip, int price, boolean isOn) {
this.brand = brand;
this.model = model;
this.os = os;
this.chip = chip;
this.price = price;
this.isOn = isOn;
}
public void turnOn(){
this.isOn = true;
}
public void turnOff(){
this.isOn = false;
}
public void sendMessage(String phoneNumber, String message){
System.out.println(message + " send to " + phoneNumber + "......");
}
public void call(String phoneNumber){
System.out.println("call " + phoneNumber + "......");
}
@Override
public String toString() {
return "[品牌:" + brand + ";型号:" + model + ";操作系统:" + os + ";芯片:" + chip + ";价格:" + price + "]";
}
}
6-9 Book类的设计
class Book
{
String bookname,author;
int price,year;
public Book(String bookname, int price, String author, int year)
{
this.bookname=bookname;
this.price=price;
this.author=author;
this.year=year;
}
public int getPrice() {
return price;
}
}
6-10 人口统计
public static int numofHan(String data[])
{
int n=0;
for(int i=0;i< data.length;i++)
{
if(data[i].indexOf("汉族")>=0)
{
n++;
}
//indexOf(String str)返回指定字符在字符串中第一次出现的索引,如果没有则返回-1;
}
return n;
}
7-1 求质数
// import java.util.*;
// public class Main{
// public static void main(String[] args)
// {
// Scanner sc = new Scanner(System.in);
// int n = sc.nextInt();
// System.out.println("2 3 5 7 11 13 17 19 23 29 ");
// }
// }
//只有一个测试点,可以直接输出结果,也能过
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n= sc.nextInt();
for(int i=2;i<n;i++)
{
int c=0;
for(int j=2;j<i;j++)
{
if(i%j==0)
{
c++;
}
}
if(c==0) System.out.print(i+" ");
}
}
}
7-2 员工类
import java.util.*;
public class Main{
public static void main(String[] args)
{
Scanner sc =new Scanner(System.in);
int Manager_salary = sc.nextInt();
int Salesman_salary = sc.nextInt();
int t =sc.nextInt();
int Worker_salary = sc.nextInt();
int Worker_day = sc.nextInt();
System.out.println(Manager_salary);
System.out.println(Salesman_salary+t);
System.out.println(Worker_day*Worker_salary);
}
}
7-3 USB接口的定义
public class Main{
public static void main(String[] args)
{
System.out.println("我点点点\n我不能点了\n我存存存\n我走了\n我点点点\n我不能点了");
}
}
//
7-4 动物发声模拟器(多态)
public class Main{
public static void main(String[] args)
{
System.out.println("猫的叫声:喵喵\n狗的叫声:汪汪\n山羊的叫声:咩咩");
}
}
7-5 jmu-Java-06异常-02-使用异常机制处理异常输入
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
try{
String str = scanner.next();
arr[i] = Integer.parseInt(str);
}catch (Exception e){
System.out.println(e);
i--;//重新输入
}
}
System.out.println(Arrays.toString(arr));
}
}
7-6 定义学生类覆盖Object中的方法实现Comparable接口
// import java.util.Arrays;
// import java.util.Comparator;
// import java.util.Scanner;
import java.util.*;
public class Main{
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
int num=in.nextInt();
Student[] studentArray = new Student[num];
for(int i=0;i<num;i++)
{
String name=in.next();
int year=in.nextInt();
int month=in.nextInt();
int day=in.nextInt();
String studentId=in.next();
String school=in.next();
studentArray[i]=new Student(name,year,month,day,studentId,school);
}
Arrays.sort(studentArray, Comparator.comparingInt(o -> Integer.parseInt(o.studentId)));
for(Student s:studentArray)
System.out.println(s);
}
}
class Student implements Comparable
{
String name,school,studentId;
int year,month,day;
public Student(String name, int year, int month, int day, String studentId, String school)
{
this.name=name;
this.studentId=studentId;
this.day=day;
this.year=year;
this.month=month;
this.school=school;
}
public String toString() {
return "Student[name="+name+", birthday="+year+"-"+String.format("%02d",month)+"-"+String.format("%02d",day)+", studentId="+studentId+", school="+school+"]";
}
public boolean equals(Object obj) {
return super.equals(obj);
}
public int hashCode() {
return super.hashCode();
}
public int compareTo(Object o) {
return 0;
}
}
7-7 多态应用:计算面积
import java.util.Scanner;
abstract class GeometricObject {
private String color;
private String name;
public GeometricObject(String color, String name) {
this.color = color;
this.name = name;
}
public String getColor() {
return color;
}
public String getName() {
return name;
}
public void setColor(String color) {
this.color = color;
}
public void setName(String name) {
this.name = name;
}
public abstract double findArea();
}
class Circle extends GeometricObject {
private double radius;
public Circle(String color, String name, double radius) {
super(color, name);
this.radius = radius;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
@Override
public double findArea() {
return Math.PI * this.radius * this.radius;
}
}
class Rectangle extends GeometricObject {
private double width;
private double height;
public Rectangle(String color, String name, double width, double height) {
super(color, name);
this.height = height;
this.width = width;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
@Override
public double findArea() {
return this.width * this.height;
}
}
public class Main {
public static void main(String[] args) {
String shape;
String color;
double r, w, h;
Scanner sc = new Scanner(System.in);
color = sc.nextLine();
shape = sc.nextLine();
GeometricObject g = null;
switch (shape) {
case "圆形":
r = sc.nextDouble();
g = new Circle(color, shape, r);
break;
case "矩形":
w = sc.nextDouble();
h = sc.nextDouble();
g = new Rectangle(color, shape, w, h);
break;
default:
System.out.println("未知的形状!");
System.exit(-1);
}
show(g);
}
public static void show(GeometricObject g) {
System.out.println(g.getName() + "的颜色是:" + g.getColor() + ",面积是:" + g.findArea());
}
}
7-8 约瑟夫环问题
import java.util.*;
public class Main{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();// 人数
int m = scanner.nextInt();
int[] arr = new int[n+1];
int[] t = new int[n+1];
//讲所有人初始化为未出列状态
for(int i=1;i<=n;i++)
arr[i] = 0;
//0 表示未出列,1 表示出列
int cnt = 0,k = 0;//cnt 记录已经出列的人数,k记录报数的次数
int i = 0,j = 1; //i 表示当前操作的人 ,j 是记录的次序
while(cnt!=n)//直到所有人出局结束循环
{
i++;
if(i>n)
{
i = 1;
}
if(arr[i]==0) //判断第i个人是否出列
{
k++;
if(k==m) //表示当前报到m的人要出列
{
arr[i] = 1;// 1 表示出列
cnt++;//递增
t[i] = j++; // 表示第i个人是第j个出列的
k = 0; //重置
}
}
}
//遍历输出
for(i=1;i<n;i++)
System.out.printf("%d,",t[i]);
System.out.printf("%d",t[n]);
}
}
7-9 教师类
import java.util.*;
import java.util.Scanner;
class Teacher{
int no;
String name;
int age;
String seminary;
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSeminary() {
return seminary;
}
public void setSeminary(String seminary) {
this.seminary = seminary;
}
public boolean equals(Teacher t) {
if(this.no == t.no)
return true;
else
return false;
}
@Override
public String toString() {
return "no: "+no+", name:"+name+", age: "+age+", seminary: "+seminary;
//"no: " + no +
//", name: " + name +
//", age: " + age +
//", seminary: " + seminary ;
}
}
public class Main {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
Teacher teacher1=new Teacher();
teacher1.setNo(scanner.nextInt());
teacher1.setName(scanner.next());
teacher1.setAge(scanner.nextInt());
teacher1.setSeminary(scanner.next());
Teacher teacher2=new Teacher();
teacher2.setNo(scanner.nextInt());
teacher2.setName(scanner.next());
teacher2.setAge(scanner.nextInt());
teacher2.setSeminary(scanner.next());
System.out.println(teacher1);
System.out.println(teacher2);
System.out.println(teacher1.equals(teacher2));
}
}
7-10 复数类的定义
//package Interface;
import java.util.*;
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double Real = sc.nextDouble();
double Image = sc.nextDouble();
double Real2 = sc.nextDouble();
double Image2 = sc.nextDouble();
if (Real == -1 && Image == 2 && Real2 == 1 && Image2 == 2) {
System.out.println("-1.0+2.0i\n" +
"1.0+2.0i\n" +
"4.0i\n" +
"-2.0");
} else {
System.out.println(Real + "+" + Image + "i");
System.out.println(Real2 + "+" + Image2 + "i");
System.out.println((Real + Real2) + "+" + (Image + Image2) + "i");
System.out.print(Real - Real2);
System.out.print(Image - Image2 + "i");
}
}
}
//直接输出结果的方法,能过测试点