zxcvbnm

7-3 字母图形 (15)
import java.util.*;
public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        char ch = 'A';
        for(int i=0;i<n;i++){
            for (int j=0;j<m;j++){
                System.out.print((char)(ch+Math.abs(i-j)));
            }
            System.out.println();
        }
    }
}

7-4 古埃及探秘-金字塔 (10)
import java.util.*;
public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int[] a = new int[100];
        for (int i=0;sc.hasNext();i++){
            a[i] = sc.nextInt();
            for (int j=0;j<a[i];j++){
                for (int x=j+1;x<a[i];x++){
                    System.out.print(" ");
                }
                for (int k=0;k<2*j+1;k++){
                    System.out.print("*");
                }
                System.out.println();
            }
        }
    }
}

7-5 jmu-Java-02基本语法-04-动态数组 (2)
import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            int n = sc.nextInt();
            String[][] str = new String[n][n];
            for (int i = 0; i < n; i++) {
                for (int j = 0; j <= i; j++) {
                    String s = (i + 1) + "*" + (j + 1) + "=" + (i + 1) * (j + 1);
                    System.out.print(s);
                    str[i][j] = s;
                    if (j < i) {
                        int flag = Math.max(7-s.length(),0);
                        for (int k = 0; k < flag; k++) {
                            System.out.print(" ");
                        }
                    }
                }
                System.out.println();
            }
            int i, j;
            System.out.print("[");
            for (i = 0; i < n; i++) {
                System.out.print("[");
                for (j = 0; j <= i; j++) {
                    System.out.print(str[i][j]);
                    if (i != j)
                        System.out.print(", ");
                }
                System.out.print("]");
                if (i < n - 1) {
                    if (i != j)
                        System.out.print(", ");
                }
            }
            System.out.println("]");
        }
    }
}

R7-1 sdut-oop-2 Shift Dot(类和对象) (10)
import java.util.*;
class Point{
	int x;
	int y;
	Point() {
	}
 	Point(int x,int y){
 		this.x = x;
 		this.y = y;
 	}
 	public void move(int x1,int y1){
 		this.x = this.x+x1;
 		this.y = this.y+y1;
 	}
    public String toString() {
		return "("+x+","+y+")";
	}
}
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n;
		while(sc.hasNextLine()){
			Point sPoint = new Point(sc.nextInt(),sc.nextInt());
			n = sc.nextInt();
			while(n--!=0){
				sPoint.move(sc.nextInt(), sc.nextInt());
			}
			System.out.println(sPoint);
		}
	}
}

R7-2 jmu-java-随机数-使用蒙特卡罗法计算圆周率的值 (10)
import java.util.*;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		long seed = sc.nextLong();
        int n = sc.nextInt();
        Random r = new Random(seed);
        int count=0;
        for(int i=0;i<n;i++){
            double x = r.nextDouble()*2-1;
            double y = r.nextDouble()*2-1;
            if(Math.pow(x,2)+Math.pow(y,2)<=1)
                count++;
        }
        System.out.println(4*(double)count/n);
	}
}

R7-3 统计学生年龄异常的人数。 (10)
import java.util.*;
class Student{
    String name;
    int age;
    static int n = 0;
    Student(String name,int age){
        if(age<=0) n++;
        this.name = name;
        this.age = age;
    }
}
    public class Main{
        public static void main(String[] args){
            Scanner sc = new Scanner(System.in);
            String str[] = new String[5];
            int[] b = new int[5];
            for(int i=0;i<5;i++){
                str[i] = sc.next();
                b[i] = sc.nextInt();
                Student S = new Student(str[i],b[i]);
            }
            if(Student.n==5)
                System.out.println("all wrong");
            else if(Student.n==0)
                System.out.println("right");
            else{
                System.out.println(Student.n);
                for(int j=0;j<5;j++){
                    if(b[j]<=0)
                        System.out.println(str[j]);
                }
            }
        }
    }

R7-4 求前N(30)
import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int i=0,a=0,b=0,c=0,n=0,f=0,g=0;
		Scanner sc = new Scanner(System.in);
		a=sc.nextInt();
		if(!(a>=1820&&a<=2020))
		{
			System.out.println("Wrong Format");
		}
		else
		{
			b=sc.nextInt();
			if(!(b>=1&&b<=12))
			{
				System.out.println("Wrong Format");
			}
			else
			{
				c=sc.nextInt();
				switch(b)
				{
					case 12:
					case 10:
					case 8:
					case 7:
					case 5:
					case 3:
					case 1:
						if(!(c>=1&&c<=31))
						g=1;
						break;
					case 11:
					case 9:
					case 6:
					case 4:
						if(!(c>=1&&c<=30))
							g=1;
							break;
					case 2:
						if((a%4==0&&a%100!=0)||a%400==0)
						{
							if(!(c>=1&&c<=29))
								g=1;
						}
						else
						{
							if(!(c>=1&&c<=28))
								g=1;
						}
						break;	
				}
				if(g==1)
				{
					System.out.println("Wrong Format");
				}
				else
				{
					n=sc.nextInt();
					if(!(n>=-10&&n<=10))
					{
						System.out.println("Wrong Format");
					}
					else
					{
						if(n>0)
						{
							if(n<c)
							{
								System.out.println(n+" days ago is:"+a+"-"+b+"-"+(c-n));
							}
							else if(n==c)
							{
								if((a%4==0&&a%100!=0)||a%400==0)
								f=1;
								switch(b)
								{
									case 1:
										System.out.println(n+" days ago is:"+(a-1)+"-"+12+"-"+31);
										break;
									case 11:
									case 9:
									case 8:
									case 6:
									case 4:
									case 2:
										System.out.println(n+" days ago is:"+a+"-"+(b-1)+"-"+31);
										break;
									case 3:
										if(f==1)
										System.out.println(n+" days ago is:"+a+"-"+(b-1)+"-"+29);
										else
										System.out.println(n+" days ago is:"+a+"-"+(b-1)+"-"+28);
										break;
									case 12:
									case 10:
									case 7:
									case 5:
										System.out.println(n+" days ago is:"+a+"-"+(b-1)+"-"+30);
										break;
								}
							}
							else
							{
								if((a%4==0&&a%100!=0)||a%400==0)
								f=1;
								switch(b)
								{
									case 1:
										System.out.println(n+" days ago is:"+(a-1)+"-"+12+"-"+(31-n+c));
										break;
									case 11:
									case 9:
									case 8:
									case 6:
									case 4:
									case 2:
										System.out.println(n+" days ago is:"+a+"-"+(b-1)+"-"+(31-n+c));
										break;
									case 3:
										if(f==1)
										System.out.println(n+" days ago is:"+a+"-"+(b-1)+"-"+(29-n+c));
										else
										System.out.println(n+" days ago is:"+a+"-"+(b-1)+"-"+(28-n+c));
										break;
									case 12:	
									case 10:	
									case 7:	
									case 5:
										System.out.println(n+" days ago is:"+a+"-"+(b-1)+"-"+(30-n+c));
										break;						
								}
							}
						}
						else if(n==0)
						{
							System.out.println(n+" days ago is:"+a+"-"+b+"-"+(c-n));
						}
						else
						{
							if(c-n<=31)
							{
								if((a%4==0&&a%100!=0)||a%400==0)
								f=1;
								switch(b)
								{
									case 12:
									case 10:
									case 8:
									case 7:
									case 5:
									case 3:
									case 1:
										System.out.println(n+" days ago is:"+a+"-"+b+"-"+(c-n));
										break;
									case 2:
										if(f==1)
										{
											if(c-n<=29)
											{
												System.out.println(n+" days ago is:"+a+"-"+b+"-"+(c-n));
											}
											else
											{
												System.out.println(n+" days ago is:"+a+"-"+(b+1)+"-"+(c-n-29));
											}
										}
										else
										{
											if(c-n<=28)
											{
												System.out.println(n+" days ago is:"+a+"-"+b+"-"+(c-n));
											}
											else
											{
												System.out.println(n+" days ago is:"+a+"-"+(b+1)+"-"+(c-n-28));
											}
										}
										break;
									case 11:
									case 9:
									case 6:
									case 4:
										if(c-n<=30)
										{
											System.out.println(n+" days ago is:"+a+"-"+b+"-"+(c-n));
										}
										else
										{
											System.out.println(n+" days ago is:"+a+"-"+(b+1)+"-"+(c-n-30));
										}
										break;																																								
								}
							}
							else
							{
								if((a%4==0&&a%100!=0)||a%400==0)
									f=1;
									switch(b)
									{
										case 10:
										case 8:	
										case 7:	
										case 5:	
										case 3:	
										case 1:
											System.out.println(n+" days ago is:"+a+"-"+(b+1)+"-"+(c-n-31));
											break;
										case 2:
											if(f==1)
											{											
												System.out.println(n+" days ago is:"+a+"-"+(b+1)+"-"+(c-n-29));
											}
											else
											{
												System.out.println(n+" days ago is:"+a+"-"+(b+1)+"-"+(c-n-28));
											}
											break;
										case 11:
										case 9:
										case 6:
										case 4:
											System.out.println(n+" days ago is:"+a+"-"+(b+1)+"-"+(c-n-30));
											break;																																																																																							
										case 12:	
											System.out.println(n+" days ago is:"+(a+1)+"-"+1+"-"+(c-n-31));
											break;
									}
							}
						}
					}
				}
			}
		}
	}
}

R7-5 jmu-Java-03面向对象基础-02-构造函数与初始化块 (5)
import java.util.*;
class Person{
	static {
		System.out.println("This is static initialization block");
	}	
	private String name;
	private int age;
	private boolean gender;
	private int id;
	private static int count=0;
	Person(){
        System.out.println("This is constructor");
        System.out.println(name+","+age+","+gender+","+id);
        System.out.println(toString());
        }
	Person(String name,int age,boolean gender){
		this.name=name;
		this.age=age;
		this.gender=gender;
	}
	public String toString() {
		return "Person [name=" + name + ", age=" + age + ", gender=" + gender + ", id=" + id + "]";
	}
	{
		this.id=count;
		System.out.println("This is initialization block, id is "+id);
		count++;
	}
}

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		String[] arr = new String[n];
		for(int i=0;i<n;i++) {
			String name = sc.next();
			int age = Integer.valueOf(sc.next());
			boolean gender = Boolean.valueOf(sc.next());
			Person p = new Person(name,age,gender);
			arr[i] = p.toString();
		}
		for(int i=arr.length;i>0;i--) {
			System.out.println(arr[i-1]);
		}
		new Person();
		sc.close();
		
	}
}

R7-6 sdust-Java-学生成绩读取与排序 (10)
import java.util.*;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        ArrayList<Student> stu=new ArrayList<Student>();
        boolean f=true;
        while(true){
            f=true;
            String[] s= sc.nextLine().split(",");
            if (s[0].equals("exit"))break;
                for (Student ss:stu){
                    if (ss.getNum().equals(s[1])){
                        ss.setScore(((ss.getScore()+Double.parseDouble(s[3]))/2));
                        f=false;
                    }
                }
                if (f) stu.add(new Student(s[0],s[1],s[2],Double.parseDouble(s[3])));
        }
        stu.sort(new Newcompartor());
        int i=1;
        for (Student s:stu){
            System.out.println("No"+i+":"+s.toString());
            i++;
        }
    }


}

class Student{
    private String name;
    private String num;
    private String clazz;
    private double score;

    public Student(String name, String num, String clazz, double score) {
        this.name = name;
        this.num = num;
        this.clazz = clazz;
        this.score = score;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getNum() {
        return num;
    }

    public void setNum(String num) {
        this.num = num;
    }

    public String getClazz() {
        return clazz;
    }

    public void setClazz(String clazz) {
        this.clazz = clazz;
    }

    public double getScore() {
        return score;
    }

    public void setScore(double score) {
        this.score = score;
    }

    @Override
    public String toString() {
        return  num  +","+ name;
    }
}

class Newcompartor implements Comparator<Student>{
    @Override
    public int compare(Student o1, Student o2) {
        double ret= o1.getScore()-o2.getScore();
        if (ret>0)ret=-1;
        else if (ret<0)ret=1;
        else{
            ret=Integer.parseInt(o1.getNum())-Integer.parseInt(o2.getNum());
            if (ret>0){
                ret=1;
            }else if (ret<0){
                ret=-1;
            }
        }
        return (int)ret;
    }
}
R7-1 jmu-Java-03面向对象基础-04-形状-继承 (15)
import java.util.*;

abstract class Shape {
	double PI = 3.14;
	public abstract double getPer();
	
	public abstract double getArea();
}

class Rec extends Shape {
	int wide,len;
	
	Rec(int a, int b){
		wide = a;
		len = b;
	}
	public double getPer(){
		return 2*(wide+len);
	}
	public double getArea(){
		return wide*len;
	}
	public  String toString() {
		return "[width=" + wide + ", length=" + len + "]";
	}
}

class C extends Shape{
	int r;
	C(int R){
		r=R;
	}
	public double getPer() {
		return r*2*PI;
	}
	public double getArea() {
		return r*r*PI;
	}
	public String toString() {
		return "[radius=" + r + "]";
	}
}

public class Main{
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		Shape A[] = new Shape[n];
		sc.nextLine();
		double sum=0,num=0;
		for(int i=0;i<n;i++){
			String S = sc.next();
			if(S.equals("rect")){
				int wide = sc.nextInt();
				int len = sc.nextInt();
				sc.nextLine();
				A[i] = new Rec(wide,len);
			}else if(S.equals("cir")){
				int r = sc.nextInt();
				sc.nextLine();
				A[i] = new C(r);
			}
			sum+=A[i].getArea();
			num+=A[i].getPer();
		}
		System.out.println(num);
		System.out.println(sum);
		
		System.out.print("[");
		for(int i=0;i<n;i++){
			if(i!=0) System.out.print(", ");
			
			if(A[i] instanceof Rec){
				System.out.print("Rectangle ");
				System.out.print(A[i].toString());
			}else{
				System.out.print("Circle ");
				System.out.print(A[i].toString());
			}
		
		}
 
		System.out.println("]");
 
		for(int i = 0;i < n;i++) {
			if(A[i] instanceof Rec) {
				System.out.println("class Rectangle,class Shape");
			}else {
				System.out.println("class Circle,class Shape");
 
			}
		}

	}
}
R7-4 jmu-java-m05-自定义Judgeable接口 (10)
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

class Person {
    String name;
    boolean gender;
    String birthdate;

    Person(){
        this.name = "null";
        this.birthdate = "";
        gender = false;
    }

    Person(String name, boolean gender){
        this.name = name;
        this.gender = gender;
    }

    Person(String name, boolean gender, String birthdate){
        this.name = name;
        this.gender = gender;
        this.birthdate = birthdate;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setBirthdate(String birthdate) {
        this.birthdate = birthdate;
    }

    public void setGender(boolean gender) {
        this.gender = gender;
    }

    public String getName() {
        return name;
    }

    public String getBirthdate() {
        return birthdate;
    }

    public boolean isGender() {
        return gender;
    }
}

interface Judgeable{
    boolean judge(String s);
    static int countPerson(List<Person> personList, Judgeable judger){
        return 0;
    }
}
public class Main {

    public static int countPerson(List<Person> personList, Judgeable judger) {
        int n = 0;
        for (Person person : personList) {
            if (judger.judge(person.getName()))
                n++;
        }
        return n;
    }


    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        List<Person> personList = new ArrayList<Person>();
        String name;
        boolean gender;
        String  birthDate;

        //在这里输入个数
        int num = sc.nextInt();

        //循环创建person对象存入personList最后
        for(int i = 1; i <= num; i++){
            name = sc.next();
            gender = sc.next()=="true"?true:false;
            birthDate = sc.next();
            Person p = new Person(name, gender, birthDate);
            personList.add(p);
        }
        //注意:当name输入为null时,将其置为空

        int nameLength5 = countPerson(personList, new Judgeable() {
            //Judgeable的实现代码

            @Override
            public boolean judge(String s) {
                return s.length() == 5;
            }
        });

        System.out.println("Number of person with nameLength == 5:"+nameLength5);
        int nameLength7 = countPerson(personList, new Judgeable() {
            //Judgeable的实现代码

            @Override
            public boolean judge(String s) {
                return s.length() == 7;
            }
        });

        System.out.println("Number of person with nameLength == 7:"+nameLength7);
        int nameisnull = countPerson(personList, new Judgeable() {
            //Judgeable的实现代码

            @Override
            public boolean judge(String s) {
                return s.equals("null");
            }
        });

        System.out.println("Number of person with null name:"+nameisnull);
    }
}
R7-5 图形卡片排序游戏 (40)
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class Main {
		
		public static Scanner input = new Scanner(System.in);
		public static void main(String[] args){
		ArrayList<Integer> list = new ArrayList<Integer>();
		int num = input.nextInt();
		while(num != 0){
		if(num < 0 || num > 4){
		System.out.println("Wrong Format");
		System.exit(0);
		}
		 list.add(num);
		 num = input.nextInt();
		}
		DealCardList dealCardList = new DealCardList(list);
		if(!dealCardList.validate()){
		System.out.println("Wrong Format");
		System.exit(0);
		}
		dealCardList.showResult();
		input.close();
		}
	}
class Card{
	 Shape shape;
	Card(){
		
	}
	Card(Shape shape){
		this.shape=shape;
	}
	public Shape getShape() {
		return shape;
	}
	public void setShape(Shape Shape) {
		this.shape=shape;
	}
	
}
class DealCardList{
	ArrayList<Card> cardList=new ArrayList<Card>();
	DealCardList(){
		
	}
	DealCardList(ArrayList<Integer> list){
		for(int i=0;i<list.size();i++)
		{
			if(list.get(i)==1)
			{
				double r=Main.input.nextDouble();
				Circle circle=new Circle(r);
				Card card=new Card(circle);
				card.getShape().setShapeName("Circle");
				cardList.add(card);	
			}
			if(list.get(i)==2) {
				double a=Main.input.nextDouble();
				double b=Main.input.nextDouble();
				Rectangle rectangle=new Rectangle(a,b);
				Card card=new Card(rectangle);
				card.getShape().setShapeName("Rectangle");
				cardList.add(card);
			}
			if(list.get(i)==3) {
				double a=Main.input.nextDouble();
				double b=Main.input.nextDouble();
				double c=Main.input.nextDouble();
				Triangle triangle=new Triangle(a,b,c);
				Card card=new Card(triangle);
				card.getShape().setShapeName("Triangle");
				cardList.add(card);
			}
			if(list.get(i)==4) {
				double a=Main.input.nextDouble();
				double b=Main.input.nextDouble();
				double c=Main.input.nextDouble();
				Traperoid traperoid=new Traperoid(a,b,c);
				Card card=new Card(traperoid);
				card.getShape().setShapeName("Trapezoid");
				cardList.add(card);
			}
			}
	}
	public boolean validate() {
		for(int i=0;i<cardList.size();i++)
			{if(!cardList.get(i).getShape().vaildate())
					return false;}
		return true;
	}
	public void cardSort() {
		for(int k=0;k<cardList.size();k++)
			for(int i=k+1;i<cardList.size();i++)
			{
				if(cardList.get(k).getShape().getArea()<cardList.get(i).getShape().getArea())
					Collections.swap(cardList, k, i);
			}
		
		
	}
	public double getAllArea() {
		double s=0;
		for(int i=0;i<cardList.size();i++)
		s=s+cardList.get(i).getShape().getArea();
		return s;
	}
	public void showResult() {
		System.out.println("The original list:");
		for(int i=0;i<cardList.size();i++)
			System.out.print(cardList.get(i).getShape().getShapeName()+":"+String.format("%.2f",cardList.get(i).getShape().getArea())+" ");
		System.out.println();
		System.out.println("The sorted list:");
		cardSort();
		for(int i=0;i<cardList.size();i++)
		System.out.print(cardList.get(i).getShape().getShapeName()+":"+String.format("%.2f",cardList.get(i).getShape().getArea())+" ");
		System.out.println();
		System.out.println("Sum of area:"+String.format("%.2f",getAllArea()));
	}
}
 class Shape {
	private String shapeName;
	Shape(){
		
	}
	Shape(String shapeName){
		this.shapeName=shapeName;
	}
	public String getShapeName() {
		return shapeName;
	}
	public void setShapeName(String shapeName) {
		this.shapeName=shapeName;
	}
	public double getArea() {
		return 0.0;
	}
	public boolean vaildate() {
		return true;
	}
	
}
class Circle extends Shape{
	private double radius;
	Circle(){
		
	}
	Circle(double radius){
		this.radius=radius;
	}
	public double getArea() {
		return Math.PI*radius*radius;
	}
	public boolean vaildate() {
		if(radius>0)
		return true;
		else return false;
	}
}
class Rectangle extends Shape{
	private double width,length;
	Rectangle (double width,double length){
		this.width=width;
		this.length=length;
	}
	public double getArea() {
		return width*length;
	}
	public boolean vaildate() {
		if(width>0&&length>0)
		return true;
		else return false;
	}
	
}
class Triangle extends Shape{
	 double side1,side2,side3;
	 Triangle(double side1,double side2,double side3){
		 this.side1=side1;
		 this.side2=side2;
		 this.side3=side3;
	 }
	 public double getArea() {
		 double c=(side1+side2+side3)/2;
		 double s=Math.sqrt(c*(c-side1)*(c-side2)*(c-side3));
			return s;
		}
	 public boolean vaildate() {
			if(side1+side2>side3&&side1+side3>side2&&side2+side3>side1)
			return true;
			else
				return false;
		}

	
}
class Traperoid extends Shape{
	private double topSide,bottomSide,height;
	Traperoid(){
		
	}
	Traperoid(double topSide,double bottomSide,double height){
		this.bottomSide=bottomSide;
		this.height=height;
		this.topSide=topSide;
	}
	public double getArea() {
		return (topSide+bottomSide)*height/2;
	}
	public boolean validate() {
		if(topSide>0&&bottomSide>0&&height>0)
			return true;
		else return false;
	}
}
R7-6 雨刷程序功能扩展设计 (100)
import java.util.Scanner;
	public class Main {
	public static void main(String[] args){
		int choice = 0;
		int type=0;
		int flag=0;
		Scanner input = new Scanner(System.in);
		type=input.nextInt();
	   if(type!=1&&type!=2)
			{System.out.println("Wrong Format");
			System.exit(0);
			}
		choice = input.nextInt();
		Lever lever = new Lever(1);
		Dial dial = new Dial(1);
		if(type==1)
			{
			dial.setN(3);
			lever.setN(4);
			}
		else
			{
			dial.setN(5);
			lever.setN(5);
			}
		
		Brush brush = new Brush(0);
		Agent agent = new Agent(lever,dial,brush);
		while(choice != 0){
			flag=0;
			switch(choice){
			case 1:
				System.out.print("Lever up/");
				agent.getLever().leverUp();
				break;
			case 2:
				System.out.print("Lever down/");
				agent.getLever().leverDown();
				break;
			case 3:
				System.out.print("Dial up/");
				agent.getDial().dialUp();
				break;
			case 4:
				System.out.print("Dial down/");
				agent.getDial().dialDown();
				break;
			case 0:
				System.exit(0);
			default :
				flag=1;
				choice = input.nextInt();
				break;
			}
			
			if(flag==0){
                agent.dealSpeed();
			System.out.println(agent.getBrush().getSpeed());
			choice = input.nextInt();
            }
		}
		}
	}
class Lever {
	private int pos;
	int n;
	public Lever(){
	}
	public Lever(int pos){
	this.pos = pos;
	}
	public int getPos() {
	return pos;
	}
	public void setN(int n)
	{
	this.n=n;	
	}

	public void leverUp() {
	if(this.pos < n){
	this.pos ++;
	}
	}	public void leverDown(){
	if(this.pos > 1){
	this.pos --;
	}
	}
}
class Dial {
	private int pos;
	int n;
	public Dial(){
	}
	public Dial(int pos){
	this.pos = pos;
	}
	public int getPos() {
	return pos;
	}
	public void setN(int n)
	{
	this.n=n;	
	}
	public void dialUp() {
	if(this.pos < n){
	this.pos ++;
	}
	}
	public void dialDown(){
	if(this.pos > 1){
	this.pos --;
	}
	}
}
class Brush {
	private int speed;
	public Brush(){
	}
	public Brush(int speed){
	this.speed = speed;
	}
	public int getSpeed() {
	return speed;
	}
	public void setSpeed(int speed) {
	this.speed = speed;
	}
}
class Agent {
	private Lever lever;
	private Dial dial;
	private Brush brush;
	public Agent(){
	}
	public Agent(Lever lever,Dial dial,Brush brush){
	this.lever = lever;
	this.dial = dial;
	this.brush = brush;
	}
	public Lever getLever() {
	return lever;
	}
	public void setLever(Lever lever) {
	this.lever = lever;
	}
	public Dial getDial() {
	return dial;
	}
	
	public void setDial(Dial dial) {
	this.dial = dial;
	}
	public Brush getBrush() {
	return brush;
	}
	public void setBrush(Brush brush) {
	this.brush = brush;
	}
    
	public void dealSpeed() {
		int speed = 0;
		switch(this.lever.getPos()){
		case 1:
			System.out.print("停止"+"/"+this.dial.getPos()+"/");
		speed = 0;break;
		case 2:
			System.out.print("间歇"+"/"+this.dial.getPos()+"/");
		switch(this.dial.getPos()){
		case 1:
		speed = 4;break;
		case 2:
		speed = 6;break;
		case 3:
		speed = 12;break;
		case 4:
		speed=15;break;
		case 5:
		speed=20;break;
		}
		break;
		case 3:
			System.out.print("低速"+"/"+this.dial.getPos()+"/");
		speed = 30;break;
		case 4:
			System.out.print("高速"+"/"+this.dial.getPos()+"/");
		speed = 60;break;
		case 5:
			System.out.print("超高速"+"/"+this.dial.getPos()+"/");
		speed = 90;break;
		}
		this.brush.setSpeed(speed);
	}
}
R7-7 sdut-oop-6 计算各种图形的周长(接口与多态) (10)
import java.util.Scanner;

interface Shape{
    double length();
}

class Triangle implements Shape{
    double a;
    double b;
    double c;

    Triangle(){
        this.a = this.b = this.c = 0;
    }

    Triangle(double a){
        this.a = this.b = this.c = 0;
    }

    Triangle(double a, double b){
        this.a = this.b = this.c = 0;
    }

    Triangle(double a, double b, double c){
        this.a = a;
        this.b = b;
        this.c = c;
        if(a <= 0 || b <= 0 || c <= 0 || !(a + b > c && a + c > b && b + c > a)){
            this.a = this.b = this.c = 0;
        }
    }

    @Override
    public double length() {
        return a + b + c;
    }
}

class Rectangle implements Shape{
    double a, b;

    Rectangle(){
        this.a = this.b = 0;
    }

    Rectangle(double a){
        this.a = this.b = 0;
    }

    Rectangle(double a, double b){
        this.a = a;
        this.b = b;
        if(a <= 0 || b <= 0){
            this.a = this.b = 0;
        }
    }

    @Override
    public double length() {
        return 2 * (a + b);
    }
}

class Circle implements Shape{
    double r;

    Circle(){
        r = 0;
    }

    Circle(double r){
        this.r = r;
        if(r <= 0) this.r = 0;
    }

    @Override
    public double length() {
        return 2 * 3.14 * r;
    }
}

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        while(sc.hasNextInt()) {
            double[] l = new double[5];

            String s;
            s = sc.nextLine();
            String[] str = s.split(" ");

            int cnt = 0;
            for (String value : str) {
                l[cnt++] = Double.parseDouble(value);
            }


            if(cnt == 1){
                Circle C = new Circle(l[0]);
                System.out.printf("%.2f\n", C.length());
            }

            else if (cnt == 2){
                Rectangle r = new Rectangle(l[0], l[1]);
                System.out.printf("%.2f\n", r.length());
            }
            else{
                Triangle t = new Triangle(l[0], l[1], l[2]);
                System.out.printf("%.2f\n", t.length());
            }
        }
    }
}
R7-8 sdut-oop-7 答答租车系统(类的继承与多态 面向对象综合练习) (10)

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		
		che [] c = 
			{
					new che(5,0,800),
					new che(5,0,400),
					new che(5,0,800),
					new che(51,0,1300),
					new che(55,0,1500),
					new che(5,0.45,500),
					new che(5,2,450),
					new che(0,3,200),
					new che(0,25,1500),
					new che(0,35,2000),
			};
		
		java.text.DecimalFormat df=new java.text.DecimalFormat("0.00"); 
		int X=in.nextInt();
		
		if(X==1)
		{
			int N=in.nextInt();
			int s1=0;
			double s2=0;
			int s3=0;
			for(int i=0;i<N;i++)
			{
				int m=in.nextInt();
				int n=in.nextInt();
				s1+=c[m-1].zaikeshu*n;
				s2+=c[m-1].zaihuoliang*n;
				s3+=c[m-1].jine*n;
			}
			System.out.print(s1+" ");
			System.out.print(df.format(s2));
			System.out.print(" "+s3);
	

		}
		else 
		{
			System.out.println(0+" "+df.format(0)+" "+0);
		}
			
	}
}

class che
{
	//public String name;
	public int zaikeshu;
	public double zaihuoliang;
	public int jine;
	
	public che(int zaikeshu,double zaihuoliang,int jine)
	{
		
		this.zaikeshu=zaikeshu;
		this.zaihuoliang=zaihuoliang;
		this.jine=jine;
	}
	
}
R7-2 jmu-Java-06异常-04-自定义异常(综合) (15)
import java.util.Scanner;
 
class IllegalScoreException extends Exception{
    public IllegalScoreException() {
    }
 
    public IllegalScoreException(String message) {
        super(message);
    }
 
    public IllegalScoreException(String message, Throwable cause) {
        super(message, cause);
    }
 
    public IllegalScoreException(Throwable cause) {
        super(cause);
    }
}
class IllegalNameException extends Exception{
    public IllegalNameException() {
    }
 
    public IllegalNameException(String message) {
        super(message);
    }
 
    public IllegalNameException(String message, Throwable cause) {
        super(message, cause);
    }
 
    public IllegalNameException(Throwable cause) {
        super(cause);
    }
}
class Student{
    private String name;
    private int score;
    private int flag=0;
    public String getName() {
        return name;
    }
 
    public int getFlag() {
        return flag;
    }
 
    public void setFlag(int flag) {
        this.flag = flag;
    }
 
    public void setName(String name) {
        char c=name.charAt(0);
        if (c>='0'&&c<='9'){
            try {
                flag=1;
                throw new IllegalNameException();
            } catch (IllegalNameException e) {
                System.out.println("IllegalNameException: the first char of name must not be digit, name="+name);
            }
 
            return;
        }
        this.name = name;
    }
 
    public int getScore() {
        return score;
    }
 
    public void setScore(int score)  {
        if (flag==1){
            return;
        }
        if (score<0||score>100){
            try {
                flag=1;
                throw new IllegalScoreException();
            } catch (IllegalScoreException e) {
                System.out.println("IllegalScoreException: score out of range, score="+score);
            }
        }
        this.score = score;
    }
    public int addScore(int score) {
        return 0;
    }
    public String toString() {
        return "Student [" +
                "name=" + name  +
                ", score=" + score +
                ']';
    }
}
public class Main {
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        while (true){
            String str=sc.nextLine();
            if (str.equals("new")){
                String s=sc.nextLine();
                String[] s1 = s.split(" ");
                if (s1.length==2){
                    String name=s1[0];
                    int score=Integer.valueOf(s1[1]);
                    Student student = new Student();
                    student.setName(name);
                    student.setScore(score);
                    if (student.getFlag()==0){
                        System.out.println(student);
                    }
                }else{
                    System.out.println("java.util.NoSuchElementException");
                }
 
            }else {
                break;
            }
        }
        sc.close();
        System.out.println("scanner closed");
    }
}
R7-1 测试抛出异常 (10)
import java.util.*;
public class Main {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner in=new Scanner(System.in);
		int n=in.nextInt();
		int[] a=new int[n];
		for(int i=0;i<n;i++){
			a[i]=in.nextInt();
		}
		
		int m=in.nextInt();
		ArrayUtil aaa=new ArrayUtil();
		for(int i=0;i<m;i++){
			int low=in.nextInt();
			int high=in.nextInt();
			aaa.findMax(a,low,high);
		}
	}
}

class ArrayUtil{
	public ArrayUtil() {
		
	}
	public void findMax(int[] arr, int begin, int end) {
		if(begin>=end) {
			System.out.println("java.lang.IllegalArgumentException: begin:"+begin+" >= end:"+end);
		}else if(begin<0) {
			System.out.println("java.lang.IllegalArgumentException: begin:"+begin+" < 0");
		}else if(end>arr.length) {
			System.out.println("java.lang.IllegalArgumentException: end:"+end+" > arr.length");
		}else{
			int max=-9999;
			for(int i=begin;i<end;i++) {
				if(arr[i]>max) {
					max=arr[i];
				}
			}
			System.out.println(max);
		}
	}
}
R7-2 较为复杂情况下的求和-hebust (10)
import java.util.Scanner;

public class Main{
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int flag=0,sum=0;
		String s = sc.nextLine();
		String[] str = s.split(" ");
		for (int i = 0; i < str.length; i++) {
			try {
        		int num = Integer.valueOf(str[i]);
                sum+=num;
        	}catch (Exception a) {
        		flag=1;
        	} 
		}
		System.out.println(sum);
		
	}
}
R7-3 设计一个Tiangle异常类 (20)
import java.util.*;
public class Main{
  public static void main(String[] args) {
      Scanner input = new Scanner(System.in);
      double s1 = input.nextDouble();
      double s2 = input.nextDouble();
      double s3 = input.nextDouble();
      try {
         Triangle t = new Triangle(s1,s2,s3);
         System.out.println(t);
      }catch (IllegalTriangleException ex) {
          System.out.println(ex.getMessage());
      }
  }
}
class IllegalTriangleException extends Exception{
	    private double side1;
		private double side2;
		private double side3;
		
		public IllegalTriangleException(double side1,double side2 ,double side3) {
			super("Invalid: "+side1+","+side2+","+side3);//message是父类成员变量,使用super向上传参!
			this.side1=side1;
			this.side2=side2;
			this.side3=side3;
		}
}
class Triangle{
    
	    private double side1;
		private double side2;
		private double side3;
		
		public Triangle(double side1,double side2,double side3) 
		  throws IllegalTriangleException{
				if((side1+side2)>side3&(side1+side3)>side2&(side2+side3)>side1) 
					{this.side1=side1;this.side2=side2;this.side3=side3;}
				else
					throw new  IllegalTriangleException(side1,side2,side3);
		}
        @Override
        public String toString(){
            return "Triangle ["+"side1="+this.side1+", side2="+this.side2+", side3="+this.side3+"]";
        }
}
R7-4 编程题:选修课成绩统计问题-hebust (12)
import java.util.*;

public class Main {
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        String[] strings = sc.nextLine().split(",");
        StringBuilder sb=new StringBuilder();
        int count=1;
        for (String string : strings) {
            String[] s1 = string.split(" ");
            sb.append(s1[0]).append(" ").append((grade(s1[1])+grade(s1[2])));
            if (count!= strings.length)sb.append(",");
            count++;
        }
        System.out.println(sb);
    }

    public static int grade(String s){
        int ret=0;
        String[] ss={"E","D","C","B","A"};
        for (int i = 0; i < ss.length; i++) {
            if (ss[i].equals(s)){
                ret=i+1;
                break;
            }
        }
        return ret;
    }
}
R7-5 多线程计算 (20)
import java.util.*;

public class Main {
	public static void main(String[] args) throws InterruptedException {
		Scanner se = new Scanner(System.in);
		ArrayList<Integer> num=new ArrayList<Integer>();
		int number_of_teacher =se.nextInt();
		int number_of_student =se.nextInt();
		for(int i=0;i<number_of_student*number_of_teacher;i++) {
			num.add(se.nextInt());
		}
		Score score=new Score(num,number_of_teacher, number_of_student);
		Thread t[]=new Thread[number_of_teacher];
		for(int i=0;i<t.length;i++) {
			t[i]=new Thread(score);
			t[i].start();
		}
	}
}

class Score implements Runnable{
	ArrayList<Integer> num=new ArrayList<Integer>();
	int number_of_teacher,number_of_student;

	public Score(ArrayList<Integer> num, int number_of_teacher, int number_of_student) {
		super();
		this.num = num;
		this.number_of_teacher = number_of_teacher;
		this.number_of_student = number_of_student;
	}


	public synchronized void run() {
		int sum = 0;
		for (int i = 0; i < number_of_student; i++) {
			sum = sum + num.get(0);
			num.remove(0);
		}
		System.out.println(sum);
	}
}























  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值