Java结课练习

1.输入1到100以内的前10个质数

public class hw1 {
	public static void main(String args[]){
		int count=0;
		int j;
			for(int i=2;i<=100;i++){
				for(j=2;j<i;j++){
					if(i%j==0){
						break;
					}
				}
				if(i==j){
					count++;
					if(count<=10){
						System.out.println(i);
					}
			}	
		}
	}
}

2.计算并输出1!+2!+3!+……+10!

public class hw2 {
	public static void main(String args[]) {
		int s=1;
		int s0=0;
		for(int i=1;i<=10;i++) {
			s*=i;
			s0+=s;	
			}
		System.out.println(s0);
		}	
	}

3.定义一个长方形类Rectangle,该类具有长length和宽width两种属性,并具有相应的构造方法、属性访问方法和计算周长permcter和面积area的方法,要求输出长是5宽是4的长方形的面积和周长。

public class Rectangle {
	double length;
	double width;
	public Rectangle(){}
	 public Rectangle(double L,double W){
		 this.length=L;
		 this.width=W;
	 }
	public double getlengths(){
		return this.length;
	}
	public double getwidth(){
		return this.width;
	}
	public void setlengths(double L){
		this.length=L;
	}
	public void setwidth(double W){
		this.width=W;
	}
	public double permeter(){
		return 2*(this.length+this.width);
	}
	public double area(){
		return this.length*this.width;
	}
	public static void main(String args[]){
		Rectangle r=new Rectangle(5,4);
		System.out.println(r.permeter());
		System.out.println(r.area());	
	}
}

4.定义一个父类Parents和两个子类Father类和Mother类:Parents类具有两个属性:姓名name和年龄age,该类所包含的方法除了构造方法和相应的get和set方法,还有一个print()用来打印对象的具体的信息,例如,输出“我是家长,我的名字是…,我的年龄是…岁"。

Father类增加了一个属性:爱好favor,并重写了父类Parents中的printo方法,输出“我是父亲,我的名字是...,我的年龄是…岁,我的爱好是...”;

Mother类增加了一个属性:头发的颜色haircolor, 并重写了父类Parents中的printo方法,输出 “我是母亲,我的名字是....,我的年龄是...岁,我头发的颜色是...”。

最后定义一个测试类 Test,要求显示如下信息: 

我是家长,我的名字是王瑞,我的年龄是36岁

我是父亲,我的名字是张峰,我的年龄是45岁,我的爱好是打保龄球

我是母亲,我的名字是孙娟,我的年龄是36岁,我的头发颜色是棕色

package family;

public class Parents {
	String name;
	int age;
	
	Parents(){}
	Parents(String name, int age){
		this.name = name;
		this.age = age;
	}
	
	public String getName(){
		return this.name;
	}
	public int getAge(){
		return this.age;
	}
	public void setName(String n){
		this.name=n;
	}
	public void setAge(int a){
		this.age=a;
	}
	
	void print(){
		System.out.println("我是家长,我的名字是"+name+",我的年龄是"+age+"岁");
	}
}
package family;

public class Father extends Parents {
	String favor;
	
	Father(String name, int age, String favor){
		super(name, age);
		this.favor = favor;
	}	
	void print(){
		System.out.println("我是父亲,我的名字是"+getName()+",我的年龄是"+getAge()+"岁,我的爱好是"+favor);
	}
}
package family;

public class Mother extends Parents {
	String hairColor;
	Mother(String name,int age,String hairColor){
		super(name,age);
		this.hairColor=hairColor;
	}
	void print(){
		System.out.println("我是母亲,我的名字是"+getName()+",我的年龄是"+getAge()+"岁,我头发的颜色是"+hairColor);
	}
}
package family;

public class Test {
	public static void main(String agrs[]){
		Parents parent = new Parents("王瑞",36);
		parent.print();
		Father father = new Father("张峰",45,"打保龄球");
		father.print();
		Mother mother = new Mother("孙娟",36,"棕色");
		mother.print();
    }
 }

5.(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() //将圆柱体的体积输出到屏幕

编写应用程序,创建类的对象,分别设置园的半径、圆柱体的高,计算并分别显示圆半径、圆面积、圆周长,圆柱体的体积。

package geology;

public class circle {
	private double radius;
	double pai=3.14;
	
	circle(){
		this.radius=0;
	}
	circle(double r){
		this.radius=r;
	}
	
	public double getarea(){
		return pai*this.radius*this.radius;
	}
	public double getperimeter(){
		return pai*this.radius*2;
	}
	void show(){
		System.out.println(this.radius);
		System.out.println(this.getarea());
		System.out.println(this.getperimeter());
	}
	
}
package geology;

public class cylinder extends circle {
	private double hight;
	cylinder(){}
	cylinder(double r,double h){
		super(r);
		this.hight=h;
	}
	
	public double getvolume(){
		return getarea()*this.hight;
	}
	void showvolume(){
		System.out.println(this.getvolume());
	}
}
package geology;

public class Test {
	public static void main(String args[]){
		circle c=new circle(3);
		cylinder cy=new cylinder(3,4);
		c.show();
		cy.showvolume();
	}
}

6.球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在第10次落地时,共经过多少米?第10次反弹多高?编写相应的Java程序。

public class ball {
	public static void main(String args[]){
		double h=100;
		double sum=100;
		for(int i=1;i<=10;i++){
			h=h/2;
			sum=sum+h*2;
		}
		System.out.println(sum+"米");
		System.out.println(h+"米");
	}
}

7.(斐波那契)有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子对数为多少?请用iava写出相关计算程序。 

程序分析:
兔子的规律为数列1,1,2,3,5,8,13,21....

import java.util.Scanner;

public class hw7 {
	public static void main(String args[]) {
		Scanner sc=new Scanner(System.in);
		int month=sc.nextInt();
		for(int i=1;i<=month;i++) {
			System.out.println("第"+i+"个月兔子对数为:"+fib(i));
		}
	}
	public static int fib(int num) {
		if(num<2)
			return num;
		else
			return fib(num-1)+fib(num-2);
	}
}

8.输出九九乘法表。写出相应的Java程序。

public class hw8 {
	public static void main(String args[]) {
		for(int i=1;i<=9;i++) {
			for(int j=1;j<=i;j++) {
				System.out.print(j+"x"+i+'='+j*i+' ');
			}
			System.out.println();
		}
	}
}

9.请编写程序,实现对数组 int a[J=new a[5];从键盘随意输入 5个值,进行从小到大排序,并在控制台窗口输出排完序的结果。

import java.util.Scanner;
public class hw9 {
	public static void main(String agrs[]){
			System.out.println("请输入:");
			Scanner sc = new Scanner(System.in);
			int []a = new int[5];
			for(int i=0; i<5; i++) {
				a[i]= sc.nextInt();
			}
			for(int i=0; i<5; i++) {
				int k = i;
				for(int j = i+1; j<5; j++) {
					if(a[k] > a[j] )
						k = j;
				}
				int temp = a[i];
				a[i] = a[k];
				a[k] = temp;
			}
			System.out.print("从小到大排序结果为:");
			for (int i = 0; i < a.length; i++) {
				System.out.print(a[i]+" ");
			}
		}
}

10.编写Animal接口,接口中声明run()方法
定义Bird类和Fish类实现Animal接口
编写Bird类和Fish类的测试程序,并调用其中的run()方法.

interface Animal{
	void run();
}
class Bird implements Animal{
	public void run(){	
		System.out.println("True");
	}
}
 class Fish implements Animal{
	public void run(){
		System.out.println("True");
	}
}
public class hw10 {
	public static void main(String args[]){
		Animal a;
		a=new Fish();
		a.run();
		a=new Bird();
		a.run();
	}
}

11.判断1996到2022年中哪些年份是闰年,输出并统计数量。 

public class hw11 {
	public static void main(String args[]){
		int count=0;
		for(int i =1996;i<=2022;i++){
			if(i%4==0&&i%100!=0||i%400==0){
				System.out.println(i);
				count++;
			}
		}
		System.out.println(count);
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值