java题解总结

java题解总结,第1张

java题解总结 7-1 输出n个数
import java.util.Scanner;

public class Main {
	  public static void main(String[] args){
	        Scanner sc=new Scanner(System.in);
	        int i;
	        int x=sc.nextInt();
          for(i=1;i<=x;i++){
               if(i%5==0){
                System.out.println(i);
            }
          else if(i!=x)
              System.out.print(i+" ");
          else
              System.out.print(i);
          }
	       
	    
	    }}

7-2 整数的因子
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=1;i 
7-2 整数的因子 
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=1;i 
7-3 判断素数 
import java.util.Scanner;


public class Main {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int n=scanner.nextInt();
		int m=0;
        if(n==1) {
			System.out.println("no");
			return ;
		}
		for(int i=2;i 
7-1 前n项的和1 
import java.util.Scanner;
//
public class Main {
	 public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        int i,sum=1;
        int x=sc.nextInt();
        for(i=1;i 
7-2 前n项的和2 (10 分) 
import java.text.NumberFormat;
import java.util.Scanner;
 
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        NumberFormat nf=NumberFormat.getNumberInstance();
        nf.setMaximumFractionDigits(2);
        int n=sc.nextInt();
        double sum=1.0;
        int i;
        for(i=1;i 
7-1 买鸡问题 
import java.util.Scanner;

public class Main{
	 public static void main(String[] args){
	        Scanner sc=new Scanner(System.in);
	        int n=sc.nextInt();
	        int m=sc.nextInt();
	        int count=0;
	        for(int i=0;i<=n/5;i++)
	            for(int j=0;j<=n/3;j++)
	                for(int k=0;k<=(n*3);k++) {
	                	 if(k%3==0&&(i+j+k==m)&&(5*i+3*j+k/3==n)){
		                        System.out.println(i+","+j+","+k);
		                        count++;
	                }
	                   
	           }
	     System.out.print(count);
	    }
}

7-2 数字黑洞123
import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner xn=new Scanner(System.in);
        int a=xn.nextInt();
        int b=0,c=0,d=0,f=0,h=0,e=0,g=1;
        while(g!=0){
            b=a;
            e=0;
            d=0;
            f=0;
            while(b!=0){
                c=b%10;
                b=b/10;
                if(c%2==0)
                    d++;
                else
                    e++;
                f++;
            }
            h++;
            if(d==1&&e==2&&f==3)
                g=0;
            a=(d*100)+(e*10)+f;
        }
        System.out.print(h);
    }
}
7-3 添加乘号
import java.util.Scanner;
 
public class Main {
	public static void main(String args[]){
		Scanner sc=new Scanner(System.in);
		int n = sc.nextInt();
		int m = sc.nextInt();
		int x = 1;
		int y=3;
		int index = 0;
			for(int k = 1; k<=n; k++){
				for(int j = 1; j<=n; j++){
					int sum=0;
					
					for(int i=1; i<=n; i++){
						if(i==x||i==y){
						 
							sum = sum+(i+1) * i;
							i++;
						}else{
							
							sum = sum +i;
						}
					}
					
					if(sum==m){
						System.out.println(x+","+y);
						index++;
						
					}
					y++;
			 	}
			 	x++;
			 	y = x+2;
			}
		 if(index==0){
							System.out.println("0");
	
	}
}
7-1猴子吃桃
public class Main {
	public static void main(String[] args) {
		Scanner scanner=new Scanner(System.in);
		int y=scanner.nextInt();
		int i=0;
        while(i<9){
            y=(y+1)*2;
            i++;
        }
		System.out.println(y);
	}

}


7-2 斐波那契数列第n项
public class Main {
	public static void main(String[] args) {
		Scanner scanner=new Scanner(System.in);
		int n=scanner.nextInt();
		int f1,f2,f3,i,t=0;
		f1=f2=1;
        if(n==1||n==2){
            System.out.print("1");
        }
		else{
            for(i=2;i 
7-3 皮球反d高度 
public class Main{
    public static void main(String[] args){
        Scanner xn=new Scanner(System.in);
        double a=xn.nextDouble();
        double b=a;
        for(int i=0;i<9;i++)
        {
            a=a/2;
            b+=a*2;
        }
        System.out.printf("%.2f",b);
        System.out.println();
        System.out.printf("%.2f",a/2);
    }
}

7-1 数组创建
public class 数组 {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int n=scanner.nextInt();
		int[] a=new int[n];
		for(int i = 0;i 
7-2 数据逆序输出 
import java.util.Scanner;

public class Main {
	public static void main(String[] args){
	     Scanner sc=new Scanner(System.in);
	     int n=sc.nextInt();
	     int[] a=new int[n];
	     for(int i=0;i=0;j--) {
	    	 System.out.print(a[j]+" ");
	     }
	    	
	     
}

}
7-3 判断是否有重复数据
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
 
public class Main {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		
		int len=scanner.nextInt();
		scanner.nextLine();
		String str = scanner.nextLine();
		String[] strs = str.split(" ");
		Set set = new HashSet();
		for (int i=0; i 
7-1 数组插入元素 
public class Main {
	public static void main(String[] args) {
		int i, j, t, k;
		Scanner read = new Scanner(System.in);
		int n = read.nextInt();
		int[] a;
		int[] b;
		a = new int[n];
		b = new int[n + 1];
		for (i = 0; i < n; i++) {
			a[i] = read.nextInt();
		}
 
		k = read.nextInt();
		for (i = 0; i <= n; i++) {
			if (i == n)
				b[i] = k;
			else
				b[i] = 0;
		}
		for (i = 0; i < n; i++) {
			for (j = 0; j < n; j++) {
				b[j] = a[i++];
			}
		}
		for (j = 0; j < n; j++) {
			for (i = 0; i < n - j; i++) {
				if (b[i] > b[i + 1]) {
					t = b[i];
					b[i] = b[i + 1];
					b[i + 1] = t;
				}
			}
		}
		for (j = 0; j < n; j++) {
			System.out.print(b[j] + " ");
		}
        System.out.print(b[n]);
	}
}
7-2 数组中的最大最小值
import java.util.Scanner;
 
public class Main {
	public static void main(String[] args) {
		Scanner read = new Scanner(System.in); 
		int n = read.nextInt(); 
		int[] arr;
		arr = new int[n];
		int i, j;
		for (i = 0; i < arr.length; i++) { 
			arr[i] = read.nextInt(); 
		}
		int max=arr[0],min=arr[0],max1=0,min1=0;
		for (i = 1; i max)
			{
				max=arr[i];
				max1=i;
			}
			if(arr[i] 
7-3 数组去除元素 
import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		int i;
		//1.输入数组
		int n=sc.nextInt();
		int[] num = new int[n];
		for(i=0;i 
7-1 数组排序 
import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n=sc.nextInt();
        int[] arr = new int[n];
        for(int i=0;i 
7-2 数组合并 
import java.util.Scanner;
 
public class Main {
	private static void bubbleSort(int[] arr) {
        if(arr==null || arr.length < 2 ){
            return;
        }
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = 0; j < arr.length - i -1; j++) { 
                if (arr[j] > arr[j + 1]) {
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
    }
    public static void main(String[] args) {
    	Scanner read= new Scanner(System.in);
		int i;
		int n = read.nextInt();
		int m = read.nextInt();
		int array01[] = new int[n];
		int array02[] = new int[m];
		for (i = 0; i < n; i++) {
			array01[i] = read.nextInt();
		}
		for (i = 0; i < m; i++) {
			array02[i] = read.nextInt();
		}
    	int[] totalArray = new int[array01.length + array02.length];
		for ( i = 0; i < array01.length; i++) {
			totalArray[i] = array01[i];
		}
		
		for ( i = 0; i < array02.length; i++) {
			
			totalArray[array01.length + i] = array02[i];
		}
		bubbleSort(totalArray);
		for ( i = 0; i < totalArray.length; i++) {
			System.out.print(totalArray[i] + " ");
		}
 
    }
  
}
7-3 数组重排
import java.util.*;
public class fabo {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc=new Scanner(System.in);
		int n,i,j,ou,p;
		n=sc.nextInt();
		int[] a=new int[n];
		for(i=0;i 
7-1 约瑟夫环问题 
import java.util.Scanner;

//约瑟夫环
public class Main {
	//1.思路分析
	
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int n,m,i=0,k=1,order=1;
		n=scanner.nextInt();
		m=scanner.nextInt();
		int[] a = new int[n];
		//i:待处理的小孩, k:待报的数 ,order:待出列的序号
		while(order<=n) {
			if(a[i]==0) {
				if(k==m) {
					a[i]=order++;
					k=0;
				}k++;
			}
		    i++;
			if(i==n)i=0;
		}
		for(i=0;i 
7-2 数组去重 
import java.util.Scanner;

public class Main{
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		int n= input.nextInt();
		int[] arr = new int[n+1];
		for(int i=0;i 
7-1 二维数组转置 
import java.util.Scanner;
public class Main {
	//数组的转置
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int[][] arr = new int[n][n];
		for(int i=0;i 
7-2 求对角线的和 
import java.util.Scanner;

public class Main{
	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int sum=0;
		int[][] arr = new int[n][n];
		int m,t=0;
		m=(n-1)/2;
		for(int i=0;i 
7-3 求二维数组的最大最小值 
import java.util.Scanner;
//求二维数组的最大值与最小值

public class Main{
	public static void main(String[] args) {
		//思路分析:
		//1.动态初始化数组,初始化最大值,最小值
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int Max,maxi=0,maxj=0;
		int Min,mini=0,minj=0;
		int[][] arr = new int[n][n];
		//2.输入数组
		for(int i=0;iMax) {
					Max=arr[i][j];
					maxi=i;
					maxj=j;
				}
			}
		}
		//4.求最小值
		for(int i=0;i 
7-1 求二维数组的鞍点 
import java.util.Scanner;
 public class Main {
    public static void main(String[] args) {
        Scanner scanner=new Scanner(System.in);
        int n=scanner.nextInt();
        int m=scanner.nextInt();
        int[][] a = new int[n][m];
        for(int i=0;i 
7-2 数字螺旋方阵 
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int i,j,num=1;
		
		int[][] a = new int[n][n];
		int cnt = n/2;//注意这个cnt

    for(i=0; i<=cnt; i++)
    {

        for(j=i; j=i; j--)

            a[n-i-1][j]=num++;

        for(j=n-2-i; j>=1+i; j--)

            a[j][i]=num++;
    }
        for(i=0;i 
7-3 九宫格的生成 
import java.util.Scanner;
 
public class Main {
    public static void main(String[] args){
        int n ;
        boolean mark = true;
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        while (mark){
            if (n % 2 == 0 || n < 0){
                n = sc.nextInt();
            }
            else{
                mark = false;
            }
        }
        int[][] arr = new int[n][n];
 
        int a = 0, b = n / 2;
        for (int i = 1; i <= n*n; i++){
            arr[a][b] = i;
 
            a--;
            b++;
 
            if (a < 0 && b >= n){
                a += 2;
                b--;
            }
 
            else if (a < 0){
                a = n - 1;
            }
 
            else if (b >= n){
                b = 0;
            }
 
            else if (arr[a][b] != 0){
                a += 2;
                b--;
            }
        }
 
 
        for (int[] c : arr){
            for (int d : c){
                System.out.printf("%-4d",d);
            }
            System.out.println();
        }
 
    }
 
}
6-1 方法:求n和m之间的所有素数的和
public static int fun(int b,int a){
    int sum=0,j,i,flag;
    for(i=a;i<=b;i++){
        flag=1;
        for(j=2;j<=Math.sqrt(i);j++){
            if(i%j==0){
                flag=0;
            }
        }
        if(flag==1){
            sum+=i;
           // sum=sum+i;
        }
    }
    return sum;
}
6-2 递归方法:汉诺塔问题
public static int hano(int n){
    int m=2;
    for(int i=0;i 
6-3 方法重载 
static double fun(double a,double b){
    double c;
    c=a+b;
    return c;
}
static String fun(String a,String b){
    String c;
    c=a+b;
    return c;
}

6-4 方法:引用变量做形参

编写方法fun,功能是对整型数组降序排序。

static void fun(int a[]){
    int b;
    for(int i=0;i 
6-1 定义类 
class Student{
    int sno,age;
    String name;
    public int getSno() {
        return sno;
    }
    public void setSno(int sno) {
        this.sno = sno;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}
6-1 学生、大学生、研究生类
class Student{
    int no;
    String name;
    String sex;

    public Student(int no, String name, String sex) {
        this.no = no;
        this.name = name;
        this.sex = sex;
    }
    public void print(){
        System.out.println("no: "+this.no);
        System.out.println("name: "+this.name);
        System.out.println("sex: "+this.sex);
    }
}

class CollegeStudent extends Student{
    String major;

    public CollegeStudent(int no, String name, String sex, String major) {
        super(no, name, sex);
        this.major = major;
    }
    public void print(){
        super.print();
        System.out.println("major: "+this.major);
    }
}
class GraduateStudent extends CollegeStudent{
    String supervisor;

    public GraduateStudent(int no, String name, String sex, String major, String supervisor) {
        super(no, name, sex, major);
        this.supervisor = supervisor;
    }
    public void print(){
        super.print();
        System.out.println("supervisor: "+this.supervisor);
    }
    public void doResearch(){
        System.out.println(this.name+" is doing research");
    }
}
6-2 Shape类
abstract class Shape{
    abstract double getPerimeter();
    abstract double getArea();
}
class Square extends Shape{
    double length;

    public Square(double length) {
        this.length = length;
    }

    public double getPerimeter(){

        return 4*this.length;
    }
    public double getArea(){

        return this.length * this.length;
    }
}
class Rectangle extends Square{
    double wide;

    public Rectangle(double length, double wide) {
        super(length);
        this.wide = wide;
    }

    public double getPerimeter(){
        return 2*(length+wide);
    }
    public double getArea(){
        return this.length * this.wide;
    }
}
class Circle extends Shape{
    double radius;

    public Circle(double radius) {
        this.radius = radius;
    }

    public double getPerimeter(){

        return 2*Math.PI*this.radius;
    }
    public double getArea(){

        return Math.PI*this.radius*this.radius;
    }
}
6-3 Student继承Person
int sno;

    Student(int sno,String name, int age ) {
        super(name, age);
        this.sno = sno;
    }
    public String toString(){
        return sno+","+name+","+age;
    }
6-1 toString方法
public String toString() {
        return getSno()+","+getName()+","+getAge();
    }
6-2 初识构造方法
class Student{
    public Student(){
        System.out.println("This is a constructor");
    }
}
6-3 初识构造方法 (10 分)

在Student类中添加带String参数的构造方法,用于对设置name的值。

public Student(String name){
    this.name=name;
}
6-1 动物体系
abstract class Animal{
	String name;
	String color;
	public Animal(String s1,String s2) {
		this.name=s1;
		this.color=s2;
	}
	public abstract void introduce();
}

class Dog extends Animal{
	int iq;
	
	public Dog(String s,String s2,int iq) {
		super(s,s2);
		this.iq=iq;
	}
	
	@Override
	public void introduce() {//需要实现introduce
		System.out.printf("My name is %s, my color is %s, my IQ is %dn",this.name,this.color,this.iq);
	}
	
	public void catchFrisbee() {
		System.out.println("catch frisbee");
	}
}

class Cat extends Animal{//Cat继承Animal

	String eyecolor;
	
	public Cat(String s1,String s2,String s3) {
		super(s1,s2);
		this.eyecolor=s3;
	}
	
	@Override
	public void introduce() {//实现introduce
		System.out.printf("My name is %s, my color is %s, my eyecolor is %sn",this.name,this.color,this.eyecolor);
	}
	
	public void catchMouse() {
		System.out.println("catch mouse");
	}
}

class TestAnimal{
	public static void action(Animal a) {
		if(a instanceof Dog) {//注意instanceof的用法
			Dog d=(Dog)a;
			d.catchFrisbee();
		}else if(a instanceof Cat) {
			Cat c=(Cat)a;
			c.catchMouse();
		}
	}
	
	public static void introduce(Animal a) {
		if(a instanceof Dog) {
			Dog d=(Dog)a;
			d.introduce();
		}else if(a instanceof Cat) {
			Cat c=(Cat)a;
			c.introduce();
		}
	}
}

6-2 员工的工资
import java.util.Scanner;

class Employee{
    private int salary;

    public Employee(int salary) {
        this.salary = salary;
    }

    public int getSalary() {
        return salary;
    }
}
class Manager extends Employee{
    public Manager(int salary) {
        super(salary);
    }
    public int getSalary() {
        return super.getSalary();
    }

}
class Salesman extends Employee{
    private int extra;

    public Salesman(int salary, int extra) {
        super(salary);
        this.extra = extra;
    }


    public int getSalary(){
         return super.getSalary()+this.extra;
    }
}
class Worker extends Employee{
    private int day;

    public Worker(int salary, int day) {
        super(salary);
        this.day = day;
    }


    public int getSalary(){
        return super.getSalary()* this.day;
    }
}
6-1 匿名内部类
new IODevice(){
    public void out(){
        System.out.println("this is print out");
    }
    public void getData(String msg){
        System.out.println("the print getdata:"+msg);
    }
    
}
6-1 面积求和 (5 分)
interface IGeometry{
    final double PI=3.14;
    double getArea();
}
class Rect implements IGeometry{
    public double a,b;
    Rect(double a,double b){
        this.a=a;
        this.b=b;
    }
    @Override
    public double getArea(){
        return a*b;
    }
}
class Circle implements IGeometry{
    public double r;
    Circle(double r){
        this.r=r;
    }
    @Override
    public double getArea(){
        return PI*r*r;
    }
}
class TotalArea{
    IGeometry[] tuxing;
    void setTuxing(IGeometry[] t){
        this.tuxing=t;
    }
    double computerTotalArea(){
        double sum=0;
        for(int i=0;i 
6-2 Student排序 
class Student implements Comparable{
    public String name;
    public int age;
    public Student(String name,int age){
        this.name=name;
        this.age=age;
    }
    public int compareTo(Student other) {
        if(this.ageother.age)return 1;
        return 0;

    }
    @Override
    public String toString(){
        return this.name;
    }
}
7-1 USB接口的定义
public class Main {
    public static void main(String[] args) {
       
        USB usb1=new Mouse();
        usb1.work();
        usb1.stop();
        USB[] usbs=new USB[2];
        usbs[0]=new Upan();
        usbs[1]=new Mouse();
        for(int i=0;i 
6-1 static属性成员 
class Student{
    static int count=1;
    Student(){
        System.out.printf("当前的Student总人数是:%dn",count);
        count++;
    }
}
6-2 static方法成员
public static int max(int a,int b){
		int p=0;
		
		if(a>b) p=a;
	    if(b>a) p=b;
		return p;
}
6-1 equals方法
public boolean equals(Object d){
    if(d instanceof PointA||d instanceof PointB)
        return false;
    if(d instanceof Point)
        return true;
    return false;
}
7-1 成绩录入时的及格与不及格人数统计
import java.util.Scanner;
 
public class Main {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int count=sc.nextInt();
        int pass=0;
        for (int i=0;i100){
                System.out.println(grade+"invalid!");
            }else if (grade>=60){
                pass++;
            }
        }
        System.out.println(pass);
        System.out.println(count-pass);
 
    }
}
7-2 编程题:判断闰年

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
int n;
Scanner input = new Scanner(System.in);
n=input.nextInt();
if(n%40&&n%100!=0||n%4000) {
System.out.println(“yes”);
}
else {
System.out.println(“no”);
}
}

}

7-5 分解质因数

import java.util.Scanner;

public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a=sc.nextInt();
int b=sc.nextInt();
for(int i=a;i<=b;i++) {
System.out.print(i+"=");
int temp=i;
for(int j=2;j if(temp%j==0) {
System.out.print(j+"*");
temp = temp/j;
j–;
}
}
System.out.println(temp);
}
}
}

7-6 统计最大数出现次数

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a;
int max = -1;
int count=1;
do {
a=sc.nextInt();
if(max
max=a;
count=1;
}else if(max==a)
count++;
}while(a!=0);
System.out.println("The largest number is "+max);
System.out.println("The occurrence count of the largest number is "+count);
}

}

7-10 jmu-Java-03面向对象基础-04-形状-继承

import java.awt.*;
import java.util.Arrays;
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n= sc.nextInt();
sc.nextLine();
Shape[] shapes=new Shape[n];
for (int i = 0; i < n; i++) {
String s=sc.nextLine();
if (s.equals(“rect”)){
shapes[i]=new Rectangle(sc.nextInt(), sc.nextInt());
sc.nextLine();
}else if (s.equals(“cir”)){
shapes[i]=new Circle(sc.nextInt());
sc.nextLine();
}
}
System.out.println(sumAllPerimeter(shapes));
System.out.println(sumAllArea(shapes));
System.out.println(Arrays.toString(shapes));
for (Shape s: shapes) {
System.out.println(s.getClass()+","+s.getClass().getSuperclass());
}
}

public static double sumAllArea(Shape[] ss){
    double ret=0;
    for (Shape s : ss) {
        ret += s.getArea();
    }
return ret;
}

public static double sumAllPerimeter(Shape[] ss){
double ret=0;
    for (Shape s:ss) {
        ret+=s.getPerimeter();
    }
return ret;
}

}

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

class Rectangle extends Shape{
private int width;
private int length;

public Rectangle(int width, int length) {
    this.width = width;
    this.length = length;
}

public int getWidth() {
    return width;
}

public void setWidth(int width) {
    this.width = width;
}

public int getLength() {
    return length;
}

public void setLength(int length) {
    this.length = length;
}

public double getPerimeter(){
    return 2.0*(this.length+this.width);
}

public double getArea(){
    return this.length*this.width;
}

@Override
public String toString() {
    return "Rectangle [" +
            "width=" + width +
            ", length=" + length +
            ']';
}

}

class Circle extends Shape{
private int radius;

public Circle(int radius) {
    this.radius = radius;
}

public int getRadius() {
    return radius;
}

public void setRadius(int radius) {
    this.radius = radius;
}
public double getPerimeter(){
    return 2*PI*this.radius;
}

public double getArea(){
    return PI*Math.pow(this.radius,2);
}

@Override
public String toString() {
    return "Circle [" +
            "radius=" + radius +
            ']';
}

}

7-15 USB接口的定义

public class Main {
public static void main(String[] args) {
USB usb1;
usb1=new Mouse();

	usb1.work();
	usb1.stop();
	
	USB[] usbs;
	usbs = new USB[2];
	
	usbs[0]=new UPan();
	usbs[1]=new Mouse();
	for(USB i:usbs) {
		i.work();
		i.stop();
	}
}

}
interface USB{
void work();
void stop();
}
class Mouse implements USB{
public void work() {
System.out.println(“我点点点”);
}
public void stop() {
System.out.println(“我不能点了”);
}
}
class UPan implements USB{
public void work() {
System.out.println(“我存存存”);
}
public void stop() {
System.out.println(“我走了”);
}
}
7-16 jmu-Java-03面向对象基础-01-构造函数与toString
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
int number = reader.nextInt();
Person[] per = new Person[number];
for(int i=0;i String name = reader.next();
int age = reader.nextInt();
boolean gender = reader.nextBoolean();
per[i]=new Person(name,age,gender);
}
for(int x=per.length-1;x>=0;x–) {
per[x].toString();
}
per.toString();
Person s=new Person();
}

}
class Person{
private String name =null;
private int age=0;
private boolean gender = false;
private int id = 0;

public Person() {
	System.out.println("This is constructor");
	System.out.println(name+","+age+","+gender+","+id);
	System.out.println("Person [name="+name+", age="+age+", gender="+gender+", id="+id+"]");
}

public Person(String n, int a, boolean g) {
	super();
	this.name =n;
	this.age = a;
	this.gender = g;
}

@Override
public String toString() {
	System.out.println( "Person [name=" + name + ", age=" + age + ", gender=" + gender + ", id=" + id + "]");
    return name;
}

}
7-17 定义商品类,封装成员变量,输出对象
import java.util.Scanner;

public class Main{
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
String number = sc.next();
String name = sc.next();
double price = sc.nextDouble();
stuff a = new stuff(number, name, price);
System.out.println(a.getNumber()+","+a.getName()+","+a.getPrice());

}

}

class stuff{
private String number;
private String name;
private double price;
public String getNumber;
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}

public stuff(String number, String name, double price) {
	super();
	this.number = number;
	this.name = name;
	this.price = price;
}

}

7-18 定义类与创建对象

public class Main {
public static void main(String[] args) {
Person p1 = new Person(“lili”,19);
Person p2 = new Person(“lucy”, 20);
System.out.println("this person is “+p1.name+”,her age is "+p1.age);
System.out.println("this person is “+p2.name+”,her age is "+p2.age);
}

}
class Person{
String name;
int age;
public Person(String name,int age) {
this.name = name;
this.age=age;
}
}

6-1 jmu-Java-06异常-finally

System.out.println(“resource open success”);
}
catch(Exception e)
{
System.out.println(e);
}
try{
resource.close();
System.out.println(“resource release success”);
}
catch(RuntimeException e)
{
System.out.println(e);
}

6-2 jmu-Java-06异常-多种类型异常的捕获

catch(NumberFormatException e){
System.out.println(“number format exception”);
System.out.println(e);
}catch(IllegalArgumentException e){
System.out.println(“illegal argument exception”);
System.out.println(e);
}catch(Exception e){
System.out.println(“other exception”);
System.out.println(e);
}
还差java复习题的两题

7-14 教师类 (10 分)
import java.awt.*;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner s=new Scanner(System.in);
        Teacher a=new Teacher(s.nextInt(),s.next(),s.nextInt(),s.next());
        Teacher b=new Teacher(s.nextInt(),s.next(),s.nextInt(),s.next());
        System.out.println(a.toString());
        System.out.println(b.toString());
        System.out.println(a.equals(b));
    }
}

class Teacher{
    int no;
    String name;
    int age;
    String seminary;

    public int getNo() {
        return no;
    }

    public int getAge() {
        return age;
    }

    public String getName() {
        return name;
    }

    public String getSeminary() {
        return seminary;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public void setAge(int age) {
        this.age = age;
    }

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

    public void setSeminary(String seminary) {
        this.seminary = seminary;
    }
    public Teacher(int no,String name,int age,String seminary){
        this.no=no;
        this.age=age;
        this.name=name;
        this.seminary=seminary;
    }
    public boolean equals(Teacher b) {
        if(this.no==b.no&&this.name==b.name)
            return true;
        else
            return false;
    }
    public String toString(){
        return "no: "+this.no+", name:"+this.name+", age: "+this.age+", seminary: "+this.seminary;
    }
}

7-1各类字符数

import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String s=sc.nextLine();
char[] ns=s.toCharArray();

    int upCount=0;
    int lowCount=0;
    int count=0;
    for(int i=0;i=65&&ns[i]<=90){
            upCount++;
        }else if(ns[i]>=97&&ns[i]<=127){
            lowCount++;
        }else{
            count++;
        }
    }
    System.out.println(upCount);
    System.out.println(lowCount);
    System.out.println(count);
    sc.close();
}

}

7-3 编程题:统计符合条件元素的个数 (8 分)

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int num=sc.nextInt();
int jCount=0;
int oCount=0;
for (int i = 1; i <=num ; i++) {
if(i%30){
if(i%2
0){
oCount++;
}else{
jCount++;
}
}
}
System.out.println(jCount+","+oCount);
sc.close();
}
}

7-4 字母图形 (15 分)

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int m=sc.nextInt();
char num=‘A’;
for(int i=0;i for(int j=0;j System.out.print((char)(num+Math.abs(i-j)));
}
System.out.println();
}
}
}

7-7 通过键盘输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。 (10 分)

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String str=sc.nextLine();
char[] chars=str.toCharArray();
int letter=0,num=0,blank=0,other=0;
for (int i = 0; i < chars.length; i++) {
if(chars[i]>=‘a’&&chars[i]<=‘z’||chars[i]>=‘A’&&chars[i]<=‘Z’){
letter++;
}else if(chars[i]>=47&&chars[i]<=57){
num++;
}else if(chars[i]==32){
blank++;
}else{
other++;
}
}
System.out.println(“字母个数:”+letter);
System.out.println(“数字个数:”+num);
System.out.println(“空格个数:”+blank);
System.out.println(“其他字符个数:”+other);
}

}

7-8 sdust-Java-字符串集合求并集

import java.util.Iterator;
import java.util.Scanner;
import java.util.TreeSet;

public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String str=sc.nextLine();
String[] s=str.split(" ");
TreeSet s1=new TreeSet<>();
TreeSet s2=new TreeSet<>();
int i=0;
while (s1.size()<5&&i s1.add(s[i++]);
}
while (s2.size()<5&&i s2.add(s[i++]);
}
TreeSets3=new TreeSet<>();
s3.addAll(s1);
s3.addAll(s2);
Iterator it=s3.iterator();
while (it.hasNext()){
System.out.println(it.next());
}
}

}

7-9 sdust-Java-学生成绩读取与排序

import java.util.*;

class Student implements Comparable{
private String name,id;
private int subject=1;
private int score;
private double sum=0;
Student(){}
Student(String name,String id,int score){
this.name=name;
this.id=id;
this.score=score;
}
public String getid(){
return this.id;
}
public void subjectadd() {
this.subject++;
}
public void scoreadd(int score){
this.score=this.score+score;
}
public String getname() {
return this.name;
}
public void sum() {
this.sum=this.score/this.subject;
}
public int compareTo(Student o1){
Student one = (Student) o1;
if(this.sum-one.sum!=0)
return (int)(one.sum-this.sum);
else
return this.id.compareTo(one.id);
}
}

public class Main{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
List list =new ArrayList();
int i,flag=0;
String k;
String[] and =new String[5];

        while(sc.hasNext())
        {
            k=sc.next();
            and=k.split(",");
            if(k.compareTo("exit")==0)
            break;
            for(i=0;i 

}

7-12 程序改错题2

public class Main {
public static void main(String[] args) {
Animal animal = new Dog();
animal.shout();
((Dog) animal).run();
}
}

class Animal {
void shout() {
System.out.println(“animal shout!”);
}
}

class Dog extends Animal {

@Override
void shout() {
    super.shout();
    System.out.println("wangwang……");
}

void run() {
    System.out.println("Dog is running");
}
7-13 程序填空题3 (5 分)

public class Main {
public static void main(String[] args) {
Son son = new Son();
son.method();
}

}
class Parent {
Parent() {
System.out.println(“Parent’s Constructor without parameter”);
}

Parent(boolean b) {
    System.out.println("Parent's Constructor with a boolean parameter");
}

public void method() {
    System.out.println("Parent's method()");
}

}

class Son extends Parent {
private static boolean b;
Son()
{
super(b);
System.out.println(“Son’s Constructor without parameter”);
}
public void method() {
System.out.println(“Son’s method()”);
System.out.println(“Parent’s method()”);
}
}

7-14 设计一个Shape及其子类Oval (20 分)

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double m = sc.nextDouble();
double n = sc.nextDouble();
Oval oval = new Oval(m, n);
System.out.println("The area of " + oval + " is " + oval.area());
System.out.println("The perimeterof " + oval + " is " + oval.perimeter());
}
}

abstract class Shape {
protected static final double PI = 3.1415926;

abstract double area();

abstract double perimeter();

}

class Oval extends Shape {
private double a;
private double b;

public Oval(double a, double b) {
    this.a = a;
    this.b = b;
}

public Oval() {
    a = 0;
    b = 0;
}

@Override
public double area() {
    return PI * a * b;
}

@Override
public double perimeter() {
    return 2 * PI * Math.sqrt((a * a + b * b) / 2);
}

@Override
public String toString() {
    return "Oval(a:" + a + ",b:" + b + ")";
}

}

7-20 好玩的图形 (20 分)

import java.util.*;

interface Shape {
public float getArea();
public float getPerimeter();
}

class Circle implements Shape {
float radius;
public Circle(float radius) {
this.radius = radius;
}

public float getArea() {
	float ans = (float) (Math.PI * radius * radius);
	return (float) ans;
}

public float getPerimeter() {
	float ans = (float) (2 * Math.PI * radius);
	return (float) ans;
}

}

class Rectangle implements Shape {
float length, width;
public Rectangle(float length, float width) {
this.length = length;
this.width = width;
}

public float getArea() {
	float ans = length * width;
	return (float) ans;
}

public float getPerimeter() {
	float ans = 2 * (length + width);
	return (float) ans;
}

}

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

	for (int i = 0; i < n; i++) {
		int flag = input.nextInt();
		if (flag == 1) {
			float radius = input.nextFloat();
			Circle c = new Circle(radius);
			String ans1 = String.format("%.2f", c.getArea());
			String ans2 = String.format("%.2f", c.getPerimeter());
			System.out.println(ans1 + " " + ans2);
		} 
        else {
			float length = input.nextFloat();
			float width = input.nextFloat();
			Rectangle m = new Rectangle(length, width);
			String ans1 = String.format("%.2f", m.getArea());
			String ans2 = String.format("%.2f", m.getPerimeter());
			System.out.println(ans1 + " " + ans2);
		}
	}
	input.close();
}

}

7-2 sdut-顺序结构-1 利用海伦公式求三角形面积
import java.util.Scanner;

import java.util.*;

public class Main {
	 public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        double a,b,c,s;
        double area;
        while(sc.hasNextDouble()) {
        	 a = sc.nextDouble();
             b = sc.nextDouble();
             c = sc.nextDouble();
             if(a+b>c&&a+c>b&&b+c>a) {
             	s=(a+b+c)/2;
             	area=(Math.sqrt(s*(s-a)*(s-b)*(s-c)));
             	System.out.printf("%.2fn",area);
             }
             else {
             	System.out.println("Input Error!");
             }
        }
       
        
    }

}

7-3 sdut-温度转换
import java.util.Scanner;
public class Main {
         public static void main(String args[]) {
             Scanner sc=new Scanner(System.in);
             double N;
             
             double n=sc.nextDouble();
             N=5.0*(n-32)/9;
                  System.out.printf("%.2f",N) ;
       }
}
6-3 接口与多态
class Circle implements Shap{
    Double r;
    final double PI=3.14;
    public Circle(double parseDouble) {
        super();
        this.r = parseDouble;
}
    @Override
    public double f() {
        if(r<=0) return 0;
        return 2*PI*r;
}
}
class Rect implements Shap{
    Double a,b;
    public Rect(double parseDouble, double parseDouble2) {
        super();
        this.a = parseDouble;
        this.b = parseDouble2;
}
    @Override
    public double f() {
        if(a<=0||b<=0) return 0;
        return 2*(a+b);
}
}
class Tri implements Shap{
    Double a,b,c;
    public Tri(double parseDouble, double parseDouble2, double parseDouble3) {
        super();
        this.a = parseDouble;
        this.b = parseDouble2;
        this.c = parseDouble3;
}
    @Override
    public double f() {
        if(a+b
						

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5684964.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存