个人Java复习题

  1. Java中设计一个测试类,测试类中设计一个打印以下格式的三角形方法,要求可以循环打印三角形,由控制台上输入三角形层数;
    当输入层数为0时,程序退出,非数字字符时,提示“输入错误,请重新输入非零数字”信息。
import java.util.Scanner;

public class TestOne {
	public static void main(String[] args) {
	        printTriangle(input());
	        
	    }

	    public static int input() {
	        while (true) {
	            try {
	                System.out.print("请输入打印层数,输入0退出程序:");
	                Scanner sc = new Scanner(System.in);
	                int num = sc.nextInt();
	                if (num == 0) {
	                    System.out.println("程序即将退出……");
	                    System.exit(0);
	                }
	                return num;
	            } catch (Exception e) {
	                System.out.println("输入错误,请重新输入非零数字:");
	            }
	        }
	    }

	    public static void printTriangle(int level) {
	        for (int i = 0; i < level; i++) {
	            for (int j = 0; j < level - i; j++)
	                System.out.print(" ");
	            for (int j = 0; j < i + 1; j++)
	                System.out.print("* ");
	            System.out.println();
	        }
	    }
}
  1. 请以自己的姓名、学号、电话号码为基本信息组成一个以“/”为分隔符字符串,最终在控制台上输出三个信息;比如:
    String myInfo=“曾xx/20180101/xxxxxxxxxxx”;
package demo2;

public class TestTwo {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		testSplit();
	}
	public static void testSplit() {
		
		
		String  myInfo ="曾金祥/20180101/13901501234";
		String[] strArray = myInfo.split("/");
		for (int i = 0; i < strArray.length; i++) {
			if (i==1) {
				System.out.println("姓名:"+strArray[i]);
			}
			else if (i==2) {
				System.out.println("学号:"+strArray[i]);
			}
			else {
				System.out.println("电话:"+strArray[i]);
			}
		}
		}
}
  1. 自定义一个整数类,设计测试类,在测试类中定义一个方法实现两个自定义整数类对象之间的交换。
package demo3;

public class MyInt {
	
	private int value;
	
	public int getValue() {
		return value;
	}
	public void setValue(int value) {
		this.value = value;
	}
	public MyInt(){
		this.value=0;
	}
	public MyInt(int value){
		this.value=value;
	}
	public void showInt(){
		System.out.println("value="+value);
	}
}

  1. 设计一个线程,线程中可以显示当前系统时间,每间隔1秒,输出系统时间日期,时间日期格式为:“自己姓名:XXXX 年 XX 月 XX 日 星期X”。
package demo4;


import java.text.SimpleDateFormat;
import java.util.Date;

public class DispsystimeRunnable implements Runnable{
    //Runnable接口方式
    @Override
    public void run() {
        Date date = new Date();
        long time = date.getTime();
        String taskname=Thread.currentThread().getName();
        SimpleDateFormat sdf = new SimpleDateFormat(taskname+"哈哈-yyyy-MM--dd HH:mm:ss E");
        while(true){
            date.setTime(time);
            String format = sdf.format(date);
            System.out.println(format);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            time+=1000;
        }
    }
}
package demo3;

import java.text.DateFormat.Field;

public class TestThree {
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		MyInt myInt1 = new MyInt(100);
		MyInt myInt2 = new MyInt(200);
		
		boolean equal = myInt1.equals(myInt2);
		System.out.println(equal);
	
		System.out.println("交换前:myInt1="+myInt1.getValue()+",myInt2="+myInt2.getValue());
		swap(myInt1,myInt2);
		System.out.println("交换后:myInt1="+myInt1.getValue()+",myInt2="+myInt2.getValue());
	}	
		public static void swap(MyInt a,MyInt b){
			int t=a.getValue();
			a.setValue(b.getValue());
			b.setValue(t);	
		}
}
package demo4;

public class MyMain {
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		DispsystimeRunnable dispsystimeRunnable=new DispsystimeRunnable () ;
		Thread thread=new Thread (dispsystimeRunnable,"显示系统时间任务") ;
		thread.start() ;
	}
}
  1. 设计实现一个Shape接口和他的两个实现类Sqaure类和Circle类,要求如下:
    (1) Shape接口中有一个抽象方法area();方法接收一个double类型的参数,返回一个double类型的结果。
    (2) Square和Circle中实现Shape接口的area()抽象方法,分别计算正方形和原型的面积,并返回;
    (3) 定义一个测试类,创建Square和Circle类的对象,计算边长为2的正方形面积和半径为3的圆形面积。
package demo5;

public abstract class Shape {
	private int heighth;
    private int width;
    private int radius;
	public int getWidth() {
        return width;
    }

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

    public int getHeighth() {
        return heighth;
    }

    public void setHeighth(int heighth) {
        this.heighth = heighth;
    }

    public Shape(int width, int heighth) {
        this.width = width;
        this.heighth = heighth;
    }



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

    public int getRadius() {
        return radius;
    }

    public void setRadius(int radius) {
        this.radius = radius;
    }
}

package demo5;

public class Square extends Shape implements ShapeInterface{
	
	public Square(int width, int heighth) {
        super(width, heighth);
    }
    @Override
    public void area() {
        int a=getWidth()*getHeighth();
        System.out.println("矩形的面积是:"+a);
    }
}
package demo5;

public class Circle extends Shape implements ShapeInterface {
	
	public Circle(int radius) {
        super(radius);
    }
	@Override
    public void area() {
        double a=Math.PI*getRadius()*getRadius();
        System.out.println("圆的面积是:"+a);
    }
}	

package demo5;

public interface ShapeInterface {
	public void area();
}
package demo5;

public class MyMain {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Circle circle=new Circle(3);
		circle.area();
		Square square=new Square(5,5);
		square.area();
	}
}
  1. 设计一个学生Student类,包含学号及姓名两个属于属性,均是String类型,要求:
    (1)改写该类的hashCode方法和equals方法,hashCode方法直接返回学号的hashCode()码,equals方法,需要判断ID和姓名完全相同时返回true,否则返回false.
    (2)设计测试类,在测试函数中定义一个hashSet集合,并添加元素,最后把集合遍历出来,在控制台上显示结果。
package demo6;

public class Student {
	private String stuid;
	@Override
	public String toString() {
		return "Student [stuid=" + stuid + ", name=" + name + "]";
	}
	private String name;
	public String getStuid() {
		return stuid;
	}
	public void setStuid(String stuid) {
		this.stuid = stuid;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	public Student(){
		
	}
	public Student(String stuid,String name){
		this.name=name;
		this.stuid=stuid;
	}
	public  void show() {
		// TODO Auto-generated method stub
		System.out.println("学号"+stuid+" 姓名:"+name);
	}
	@Override
	public int hashCode() {
		// TODO Auto-generated method stub
		return stuid.hashCode();
	}
	@Override
	public boolean equals(Object obj) {
		// TODO Auto-generated method stub
		if (obj instanceof Student) {
			if (obj==this) return true;
			String name = ((Student)(obj)).getName();
			String id = ((Student)(obj)).getStuid();
			if (this.name.equals(name) && this.stuid.equals(id)){
				return true;
			}
			else
				return false;
			}
		else
				return false;
	}
}
package demo6;

import java.util.HashSet;
import java.util.Iterator;
public class MyMain {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		MyMain myMain=new MyMain();
		myMain.testStudentHashSet();
	
	}
	public void testStudentHashSet(){
		HashSet<Student> hashSet=new HashSet<Student>();
		hashSet.add(new Student("001","老大"));
		hashSet.add(new Student("002","老二"));
		hashSet.add(new Student("003","老三"));
		hashSet.add(new Student("004","老四"));
		hashSet.add(new Student("005","老五"));
		
		hashSet.add(new Student("001","老大"));
		Iterator<Student> it=hashSet.iterator();
		
		while(it.hasNext()){
			Student student=it.next();
			student.show();
		}
	}
}

设计一个人类 Person 和它的一个雇员子类 Employee,要求如下:
①Person类有 name (姓名) 和 age (年龄) 属性, 一个包含两个参数的构造方法,用于给 name 和 age 属性赋值,一个 show()方法打印 Person的属性信息;
②雇员类 Employee是Person类的子类,增加一个 workid(工号)属性和一个salary(工资)属性,数据类型是double,有一个包含四个参数
的构造方法, 前两个参数用于给父类的 name 和 age属性赋值, 第三、四参数分别给workid和salary赋值。重写父类的show方法用于打印 Employee类的所有属性信息;③在测试类中分别创建 Person对象和 Employee对象, 分别调用它们的show ()方法显示信息。

package demo7;

public class Person {
	private String name;
    private int age;


    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 Person() {
    }
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
    public void show(){
        System.out.println("我叫"+name+":年龄:"+age);
    }
}

package demo7;

public class Employee extends Person{
	private String workid;
    private double salary;

    public Employee(String workid, double salary) {
        this.workid = workid;
        this.salary = salary;
    }

    public Employee(String name, int age, String workid, double salary) {
        super(name, age);
        this.workid = workid;
        this.salary = salary;
    }
    public void show(){
        System.out.println("我叫"+getName()+":年龄:"+getAge()+"工号是:"+workid+",工资是:"+salary);
    }
}

package demo7;

public class MyMain {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Person person=new Person();
        person.setAge(18);
        person.setName("张三");
        person.show();
        Employee employee=new Employee("张三",20,"20",888.88);
        employee.show();
	}
}

用Java实现一个具有记事本功能的程序,要求:
(1)设计一个类,具有存储信息的集合成员变量
(2)该类中具备:增加一条信息,删除一条信息,获得记事本信息的个数,列数所有记事本信息的功能(返回值为字符串数组)等4个方法。
(3)main方法中,测试类,添加5条记事本信息,并显示出来。

package com.czie3;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Set;

public class Test {

	/**
	 * @param args
	 */
	ArrayList<String> arrayList = new ArrayList<String>();
	public  void AddMessage(String str){
		
		arrayList.add(str);
	}
	public void  RemoveMessage(String str){
		arrayList.remove(str);
	}
	public void GetMessage(){
		
		int size = arrayList.size();
		System.out.println(size);
		
	}
	public void ListMessage(){
		
		for (int i = 0; i < arrayList.size(); i++) {
			System.out.println(i+"="+arrayList.get(i));
		}
		
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		Test test =new Test();
		
		test.AddMessage("张三");
		test.AddMessage("李四");
		test.AddMessage("王五");
		test.AddMessage("赵六");
		test.AddMessage("田七");
		System.out.println("====打印添加后信息=====");
		System.out.println(test.arrayList);
		System.out.println("====打印删除后信息=====");
		test.RemoveMessage("张三");
		System.out.println(test.arrayList);
		System.out.println("====获取信息的大小=====");
		test.GetMessage();
		System.out.println("====遍历所有信息=====");
		test.ListMessage();
	}
}

用Java 设计实现一个能够存储、查询美元面值和名称之间的关系,要求:
(1)设计一个类,具有存储美元面值和名称的集合,在该类的构造方法中把以下对应关系,添加到集合中:
5, “penny”; 10, “dime”;25, “quarter”;50), "halt-dollars”
(2)具有public String getName(Integer key)方法,根据美元面值返回美元名称。
(6)main函数测试,当输入某一个面值数值时,返回当前数值的名称,没有找到,显示“not found”;

package com.czie3;

import java.util.HashMap;
import java.util.Scanner;
/**
 * 用Java 设计实现一个能够存储、查询美元面值和名称之间的关系,要求:
(1)设计一个类,具有存储美元面值和名称的集合,在该类的构造方法中把以下对应关系,添加到集合中:
5, "penny"; 10, "dime";25, "quarter";50), "halt-dollars”
(2)具有public String getName(Integer key)方法,根据美元面值返回美元名称。
(6)main函数测试,当输入某一个面值数值时,返回当前数值的名称,没有找到,显示“not found”;

 * @author dell
 *
 */

public class TestDemo {

	HashMap<Integer,String> hashMap=new HashMap<Integer,String>();
	public TestDemo(){
		
		
		hashMap.put(5,new String("penny"));
		hashMap.put(10,new String("dime"));
		hashMap.put(25,new String("quarter"));
		hashMap.put(50,new String("halt-dollars"));
		
	}
	public String getName(Integer key){
		
		if (hashMap.containsKey(key)) {
			String string = hashMap.get(key);
			return string;
		}
		else {
			return null;
		}
	}
	@SuppressWarnings("resouce")
	public static void main(String[] args) {
		TestDemo testDemo = new TestDemo();
		
		// TODO Auto-generated method stub
		Scanner sc=new Scanner(System.in);
		
		while(true){
			System.out.println("输入一个面值");
			Integer key = sc.nextInt();
		String value=testDemo.getName(key);
		if (key.equals("quit")) {
			System.exit(0);
		}
		if (value==null) {
			System.out.println(key+":not found");
		}
		else {
			System.out.println(key+"="+value);
		}
		System.out.println("程序退出");
	}
	}
	
}

设计一个表示分数的类Fraction。这个类用两个int类型的变量分别表示分子和分母。
这个类的构造函数是:
Fraction(int a, int b)
构造一个a/b的分数。
Fraction plus(Fraction r);
将自己的分数和r的分数相加,产生一个新的Fraction的对象。注意小学四年级学过两个分数如何相加的哈。
Fraction multiply(Fraction r);
将自己的分数和r的分数相乘,产生一个新的Fraction的对象。
void print();
将自己以“分子/分母”的形式输出到标准输出,并带有回车换行。如果分数是1/1,应该输出1。当分子大于分母时,不需要提出整数部分,即31/30是一个正确的输出。
注意,在创建和做完运算后应该化简分数为最简形式。如2/4应该被化简为1/2。
以下是Main.java 类的main函数测试类。
请在控制台上输入4 个数,显示结果 ,输入样例 2 4 1 3回车

import java.util.Scanner;

public class Main {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner in = new Scanner(System.in);
        Fraction a = new Fraction(in.nextInt(), in.nextInt());
        Fraction b = new Fraction(in.nextInt(), in.nextInt());
        a.print();
        b.print();
        a.plus(b).print();
        a.multiply(b).plus(new Fraction(8, 10)).print();
        a.print();
        b.print();
        in.close();
    }

    static class Fraction {
        private int fenzi;
        private int fenmu;
        double score;

        /*
         * Fraction(int a, int b)
            构造一个a/b的分数。
         */
        public Fraction(int a, int b) {
            this.fenzi = a;
            this.fenmu = b;
        }


        double toDouble() {
            return (double) fenzi / fenmu;
        }

        /**
         * Fraction plus(Fraction r);
         * 将自己的分数和r的分数相加,产生一个新的Fraction的对象。
         *
         * @param r
         * @return
         */

        public Fraction plus(Fraction r) {
            Fraction m = new Fraction(0, 0);
            m.fenmu = this.fenmu * r.fenmu;
            m.fenzi = this.fenzi * r.fenmu + this.fenmu * r.fenzi;
            return m;
        }

        /**
         * Fraction multiply(Fraction r);
         * 将自己的分数和r的分数相乘,产生一个新的Fraction的对象。
         *
         * @param r
         * @return
         */
        public Fraction multiply(Fraction r) {
            Fraction m = new Fraction(0, 0);
            m.fenzi = this.fenzi * r.fenzi;
            m.fenmu = this.fenmu * r.fenmu;
            return m;
        }

        /**
         * void print();
         * 将自己以“分子/分母”的形式输出到标准输出,并带有回车换行。
         */

        public void print() {
            int temp;
            int a = fenzi;
            int b = fenmu;

            //辗转相除法
            while (b != 0) {
                temp = a % b;
                a = b;
                b = temp;
            }

            fenzi /= a;
            fenmu /= a;

            //判断输出
            if (fenzi % fenmu != 0) {
                System.out.println(fenzi + "/" + fenmu);
            } else //if(fenzi%fenmu==0)
            {
                if (fenzi > fenmu) {
                    System.out.println(fenzi / fenmu);
                } else {
                    System.out.println(fenmu / fenzi);
                }
            }
        }
    }
}

以上代码由本人Eclipse验证无误

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值