Java基础练习


时间之矢稍纵即逝,接近2周就要Java期末考了。作为一个一学期没怎么认真听Java课的我来说,内心波澜起伏。借着空余的时间,把Java的重点稍微总结了一下,下面就以涵盖全书的4个例子展开。

题1(数组):

使用Scanner输入n个数(小于100),并将之升序输出。

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

public class haha {
	public static void main(String[] args){
		Scanner reader = new Scanner(System.in);
		int n = reader.nextInt();
		float[] xiang = new float[n];
		for(int i=0;i<n;i++) {
			xiang[i] = reader.nextFloat();
		}
		Arrays.sort(xiang);
		for(int i=0;i<n;i++) {
			System.out.println(xiang[i]);
		}
	}
}

题2(继承):

(1)编写一个圆类Circle,该类拥有:
①一个成员变量
Radius(私有,浮点型); // 存放圆的半径;
②两个构造方法
Circle( ) // 将半径设为0
Circle(double r ) //创建Circle对象时将半径初始化为r
③ 三个成员方法
double getArea( ) //获取圆的面积
double getPerimeter( ) //获取圆的周长
void show( ) //将圆的半径、周长、面积输出到屏幕
(2)编写一个圆柱体类Cylinder,它继承于上面的Circle类。还拥有:
①一个成员变量
double hight(私有,浮点型); // 圆柱体的高;
②构造方法
Cylinder (double r, double h ) //创建Circle对象时将半径初始化为r
③ 成员方法
double getVolume( ) //获取圆柱体的体积
void showVolume( ) //将圆柱体的体积输出到屏幕
编写应用程序,创建类的对象,分别设置圆的半径、圆柱体的高,计算并分别显示圆半径、圆面积、圆周长,圆柱体的体积。


Circle.java:

public class Circle {
	private double Radius;
	
	public Circle() {
		Radius=0;
	}
	
	public Circle(double r) {
		Radius=r;
	}
	
	public double getArea(){
		return Math.PI*Math.pow(Radius, 2);
	}
	
	public double getPerimeter(){
		return 2*Math.PI*Radius;
	}

	public void show() {
		System.out.println("圆半径:"+Radius);
		System.out.println("圆面积:"+this.getArea());
		System.out.println("圆周长:"+this.getPerimeter());
	}
}

Cylinder.java

public class Cylinder extends Circle {
	private double hight;
	
	public Cylinder(double r,double h){
		super(r);
		hight = h;
	}
	
	public double getVolume(){
		return super.getArea()*hight;
	}
	
	public void showVolume(){
		System.out.println("圆柱体体积:"+this.getVolume());
	}

	public static void main(String[] args) {
		Cylinder obj = new Cylinder(1,1);
		obj.show();
		obj.showVolume();
	}
}

题3(抽象类)

1.设计Person抽象类,具有年龄、性别、体重、身高等属性,其中年龄、性别为一般方法,体重、身高为抽象方法;

2.在Person抽象类基础上,进一步派生Teacher类,包含工号、系别等信息;

3.设计Teacher测试运行例子,输出相应信息。


Person.java

public abstract class Person {
	void setAge() {}
	void setSex() {}
	abstract void setWeight(double weight);
	abstract void setHeight(double height);
}

Teacher.java

public class Teacher extends Person{
	private int age;
	private String sex;
	private double weight;
	private double height;
	private String document;
	private String number;
	
	Teacher() {
		age = 0;
		sex = "x";
		document = "xxx";
		number = "xxxx";
		height = 0;
		weight = 0;
	}
	
	void setWeight(double w) {
		weight = w;
	}
	void setHeight(double h) {
		height = h;
	}
	void setAge(int a) {
		age = a;
	}
	void setSex(String s) {
	    sex = s;
	}
	void setDocument(String s) {
	    document = s;
	}	
	void setNumber(String s) {
	    number = s;
	}
	void show() {
		System.out.println("teacher");
		System.out.println("-------------------------");
		System.out.println("age:"+age);
		System.out.println("sex:"+sex);
		System.out.println("height:"+height);
		System.out.println("weight:"+weight);
		System.out.println("doucument:"+document);
		System.out.println("number:"+number);
	}
	//测试
	public static void main(String[] args) {
		Teacher t = new Teacher();
		t.setAge(4);
		t.setSex("男");
		t.setHeight(183.2);
		t.setWeight(42.5);
		t.setDocument("自动化");
		t.setNumber("1801");
		t.show();
	}
}

题4(接口):

  1. 设计Person接口,具有获取和设置年龄、性别、体重、身高等属性的方法
  2. 设计Teacher类实现Person接口,包含工号、系别等信息;实现信息输出功能;
  3. 设计Student类实现Person接口,包含学号信息;实现输出功能;
  4. 设计Student、Tercher测试运行类,体现接口的应用

Person.java

public interface Person {
	void setAge(int age);
	int getaAge();
	void setSex(String sex);
	String getSex();
	void setHeight(double height);
	double getHeight();
	void setWeight(double W);
	double getWeight();
	void show();
}

Teacher.java

public class Teacher implements Person{
	private int age;
	private String sex;
	private String document;
	private String number;
	private double height;
	private double weight;
	
	Teacher(){
		age=0;
		sex="x";
		document="xxx";
		number="xxxx";
		height=0;
		weight=0;
	}
	
	public void setAge(int a) {
		age =a;
	}
	public int getAge() {
		return age;
	}
	public void setSex(String s) {
		sex = s;
	}
	public String getSex() {
		return sex;
	}
	public void setHeight(double h) {
		height=h;
	}
	public double getHeight() {
		return height;
	}
	public void setWeight(double w) {
		weight = w;
	}
	public double getWeight() {
		return weight;
	}
	public void setDocument(String d) {
		document=d;
	}
	public void setNumber(String n) {
		number = n;
	}
	public String getNumber() {
		return number;
	}
	public String getDocument() {
		return document;
	}
	public void show() {
		System.out.println("teacher");
		System.out.println("----------------------");
		System.out.println("age:"+age);
		System.out.println("sex:"+sex);
		System.out.println("height:"+height);
		System.out.println("weight:"+weight);
		System.out.println("number:"+number);
		System.out.println("document:"+document);
	}
	public static void main(String[] args) {
		Teacher t = new Teacher();
		t.setAge(123);
		System.out.println("age:"+t.getAge());
		t.setSex("男");
		System.out.println("sex:"+t.getSex());
		t.setHeight(123.4);
		System.out.println("heght:"+t.getHeight());
		t.show();
	}
}

还剩下Student类,就…就自行解决吧!毕竟道理都是相通的啦!


文章作者: Peyton
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 Peyton !
  目录