Java练习题1

第二章 Java语言基础

1.编写一个整数数组(长度为5),从键盘上依次输入所有的元素,对数组进行排序,然后用foreach输出所有的元素。

import java.util.Arrays;
import java.util.Scanner;

public class Homework2_1 {

	public static void main(String[] args) {
		int cnt=0;
		int[] numbers= new int[5];
		Scanner in= new Scanner(System.in);
		System.out.println("请输入五个数:");
		for(cnt=0;cnt<5;cnt++) {
			numbers[cnt]=in.nextInt();
		}
		Arrays.sort(numbers);
		for(int x:numbers)
			System.out.println(x+" ");
	}

}

2.定义一个double型数组d1,并进行初始化(长度为5-10之间),编写代码实现d1的克隆,并将其赋值给数组d2,要求d2中的元素与d1完全相同,且具有自己的内存空间

public class Homework2_2 {
	
	public static void main(String[] args) {
		double[] d1= {2021,10,12,8,52};
		for(double x:d1)
		   System.out.println(x+" ");
		double[] d2= new double[5];
		System.arraycopy(d1, 0, d2, 0, 5);
		for(double x:d2)
			System.out.println(x+" ");
	}

}

3.编程比较两个String对象的大小,若字符串1和字符串2相等,显示相同;若字符串1和字符串2不相等,则显示第一个不同字符的差值;若字符串1和字符串2仅长度不同,则显示两者长度的差值(可在main方法中之前对两个String对象赋值,通过赋不同的值来测试不同的情况)。

import java.util.Scanner;

public class Homework2_3 {

	public static void main(String[] args) {
        Scanner in= new Scanner(System.in);
        System.out.println("请输入要比较的字符串");
        String s1= new String(in.nextLine());
        String s2= new String(in.nextLine());
        System.out.println(s1.compareTo(s2));
	}

}

第三章 类与对象

  1. 设计Point类用来定义平面上的一个点坐标,包含构造函数和显示信息的方法。使用泛型技术,以Point类对象作为数组元素,创建ArrayList类对象,并向其中添加3个元素,然后依次遍历集合显示信息。
import java.util.ArrayList;

class Point{
	double x,y;
	String name;
	Point(String name,double x,double y){
		this.name=name;
		this.x=x;
		this.y=y;
	}
	String getInfo() {
		return name+"的横坐标为:"+x+",纵坐标为:"+y;
	}
}

public class Homework4_3 {
	public static void main(String[] args) {
		Point point1=new Point("A",1,1);
		Point point2=new Point("B",2,2);
		Point point3=new Point("C",3,3);
		ArrayList<Point> myList = new ArrayList<Point>();
		myList.add(point1);
		myList.add(point2);
		myList.add(point3);
		for(Point x:myList) {
			System.out.println(x.getInfo());
		}
		
	}

}

1.设计Point类用来定义平面上的一个点坐标,包含向右平移和向下平移的方法。编写测试类Test,在该类中定义Point类的对象,并调用相应的函数。

public class Point {
	    double x,y;
	    String name;
		static int count;
		Point(double x,double y,String name){
			this.x=x;
			this.y=y;
			this.name=name;
			count++;
		}
	    double moveRight(double right){
	    	x=right+x;
			return x;
	    }
	    double moveDown(double down){	
	    	y=y-down;
	    	return y;
	    }
	    int getCount()
	    {
	    	return count;
	    }

}

测试类

class TestPoint {

	Point point1= new Point(1, 2, "A");
	Point point2= new Point(0,0,"B");
	
	void testPoint() {
		System.out.println(point1.name+":x="+point1.x+",y="+point1.y);
		System.out.println(point2.name+":x="+point2.x+",y="+point2.y);
	}

	void testMoveright() {
		double result=point1.moveRight(1);
		System.out.println("A向右平移1格后,x="+point1.x+",y="+point1.y);		
	}

	void testMovedown() {
		double result=point2.moveDown(1);
		System.out.println("B向下平移1格后,x="+point2.x+",y="+point2.y);		
	}

}

2.编写复数类,为该类定义信息输出方法、两个复数相加的方法,在测试类中完成2个复数对象的构建完成相加并赋值给第3个复数对象。

import java.util.Scanner;
public class Complex {  
    private double real;
    private double image;

    public Complex(double real,double image){
        this.real = real;
        this.image = image;
    }
    public Complex(String s){  //字符串构造复数1+2i/2/3i/a-i/a+i
        int op = -1;  //存储运算符的位置
        int i_index = -1;  //存储i的位置
        int len = s.length();
        String a = "";  //实部
        String b = "";  //虚部
        if (s!=null&&!"".equals(s)){
            for (int i=0;i<len;i++){
                if (s.charAt(i) == '+'||s.charAt(i) == '-')  op = i;  //记录实部虚部连接号
                if (s.charAt(i) == 'i')  i_index = i;  //记录虚部后面的i
            }
            if(i_index==-1){  //不含i,只有实部
                a = s;
                b = "0";
            }else if (op==-1&&i_index!=-1){
                if (i_index==0){  //i
                    a = "0";
                    b = "1";
                }else{  //2i
                    a = "0";
                    b = s.substring(0,i_index);
                }
            }else if (op==0&&i_index!=1){  //-i
                    if (i_index==1){
                        a = "0";
                        b = "-1";
                    }else{
                        a = "0";
                        b = s.substring(0,i_index);
                    }
            }else if (i_index - op==1){  //虚部系数为1或-1
                a = s.substring(0,op);
                b = s.charAt(op) + "1";
                if ("".equals(a)) a = "0";
            }else if (i_index!=-1&&op!=-1){  //含i,不含op,实部为正且不含实部
                a = s.substring(0,op);
                b = s.substring(op,i_index);
                if ("".equals(a)) a = "0";
                if ("".equals(b)) b = "0";
            }
            Set(Double.valueOf(a),Double.valueOf(b));
        }
    }
    public String toString(){  //字符串描述
        if (this.real==0&&this.image==0){
            return "z = 0.0";
        }
        if (this.real == 0){  //只有虚部
            return "z = "+this.image+"i";
        }
        if (this.image == 0){  //只有实部
            return "z = "+String.valueOf(this.real);
        }
        if (this.image < 0){  //虚部为负
            return "z = "+this.real+"+("+this.image+"i)";
        }
        if (this.image > 0){  //虚部为正
            return "z = "+this.real+"+"+this.image+"i";
        }
        return null;
    }
    
    public void Set(double a,double b){
        this.real = a;
        this.image = b;
    }
    
    public String Add(Complex another){  //复数加法
        double add_x = this.real + another.real;
        double add_y = this.image + another.image;
        Complex z3= new Complex(add_x,add_y);
        return z3.toString();
    }

测试类

class TextComplex{
public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("请输入第一个复数:");
        String s1 = in.next();
        System.out.println("请输入第二个复数:");
        String s2 = in.next();
        Complex z1 = new Complex(s1);
        Complex z2 = new Complex(s2);
        System.out.println(z1.Add(z2));
        }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Asio otus

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

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

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

打赏作者

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

抵扣说明:

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

余额充值