博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
面向对象的小练习题(一)
阅读量:3968 次
发布时间:2019-05-24

本文共 5733 字,大约阅读时间需要 19 分钟。

1猜数字游戏一个类A有一个成员变量v。定义一个类对A类的成员变量v进行猜。如果大了则提示大了小了则提示 小了。等于则提示猜测成功。

解法一:

import java.util.Random;import java.util.Scanner;public class A {	private Integer vv = 0;	public A() {		Random r = new Random();		vv = r.nextInt(1000);	}	public String guess (Integer parameter1) {		String res = "小了";		if(parameter1!=null) {			if(vv.compareTo(parameter1)==0)				res = "相等";			else if(vv.compareTo(parameter1)<0)				res = "大了";		}		return res;	}	public static void main(String[] args) {		A a = new A();		Scanner sc =new Scanner(System.in);		while(true) {			System.out.println("input:");			String ss = sc.nextLine();			if("quit".equalsIgnoreCase(ss))				break;			try {				int kk = Integer.parseInt(ss);				String res = a.guess(kk);				if("相等".equals(res)) {					System.out.println("reight…………");					break;				}				else {					System.out.println(res);				}			} catch (Exception e) {				System.out.println("不合法的輸入");			}					}	}}

解放二:

import java.util.Random;import java.util.Scanner;public class A {	private int v=0;	public A() {		Random r = new Random();		v = r.nextInt(1000);	}	public int Guess() {		Scanner sc = new Scanner(System.in);		System.out.println("請輸入你猜的數:");		int xx  = sc.nextInt();		while(true) {						if(xx>v) {				System.out.println("猜大了!");			}			if(xx

2.请定义一个交通工具Vehicle的类其中有: 属性速度speed体积size等 方法移动move()、设置速度setSpeed(int speed)、加速speedUp()、减速speedDown()等 后在测试类Vehicle中的main()中实例化一个交通工具对象并通 过方法给它初始化speed,size的值并且通过打印出来。另外调用加速减速的方法对速度进行改变。

public class Vehicle {	private double speed = 0;	private double size;	public void move() {		System.out.println("我使劲的跑!");	}	public void setPeed(double speed) {		if (speed < 0)			this.speed = 0;		else			this.speed = speed;	}	public void speedUp() {		this.speed++;	}	public void speedDown() {		this.speed--;	}	public static void main(String[] args) {		Vehicle v = new Vehicle();		v.setPeed(100);		v.size = 200;		System.out.println("交通工具:速度为:" + v.speed + "体积为:" + v.size);		v.speedUp();		System.out.println("交通工具:速度为:" + v.speed + "体积为:" + v.size);		v.speedDown();		System.out.println("交通工具:速度为:" + v.speed + "体积为:" + v.size);	}}

3.在程序中经常要对时间进行操作但是并没有时间类型的数据。那么可以自己实现一个时间类来满足程序中的需 要。 定义名为MyTime的类,其中应有三个整型成员时hour分、minute秒、second。为了保证数据的安全性这三 个成员变量应声明为私有。为MyTime类定义构造方法以方便创建对象时初始化成员变量。

(1)再定义diaplay方法用于将时间信息打印出来。

(2)为MyTime类添加以下方法:addSecond(int sec)、addMinute(int min)、addHour(int hou)、 subSecond(int sec)、subMinute(int min)、 subHour(int hou) 分别对时、分、秒进行加减运算

public class MyTime {	private int hour;	private int minute;	private int second;	public String display() {		return hour + "时" + minute + "分" + second + "秒";	}	public void addSecond(int second) {		this.second += second;		validSecond();		validMinute();		validHour();	}	public void addMinute(int minute) {		this.minute += minute;		validMinute();		validHour();	}	public void addHour(int hour) {		this.hour += hour;		validHour();	}		public void subSecond(int second) {		addSecond(-1*second);	}	public void subMinute(int minute) {		addMinute(-1*minute);	}	public void subHour(int hour) {		addHour(-1*hour);	}	private void validSecond() {		if (this.second > 0) {			int x = this.second / 60;			this.second %=  60;			if (x > 0)				this.minute += x;			}else if(this.second<0) {			double d1 = this.second / 60.;			int d2 = (int) Math.floor(d1);			this.minute += d2;			this.second += Math.abs(d2) * 60;		}	}	private void validMinute() {		if (this.minute > 0) {			int x = this.minute / 60;			this.second %=  60;			if (x > 0)				this.hour += x;		}		else if(this.minute<0) {			double d1 = this.minute / 60.;			int d2 = (int) Math.floor(d1);			this.hour += d2;			this.minute += Math.abs(d2) * 60;		}	}	private void validHour() {		if (this.hour > 0) {			this.hour %= 24;		} else {			double d1 = this.hour / 24.;			int d2 = (int) Math.floor(d1);			this.hour += Math.abs(d2) * 24;		}	}	public static void main(String[] args) {		MyTime x = new MyTime();		x.addSecond(900);		x.addMinute(5);		x.addHour(5);		System.out.println(x.display());	}}

4.编写Java程序模拟简单的计算器。 定义名为Number的类,其中有两个整型数据成员n1和n2应声明为私有。编 写构造方法赋予n1和n2初始值再为该类定义加addition、减subtration、乘multiplication、除division等公有成员 方法,分别对两个成员变量执行加、减、乘、除的运算。 在main方法中创建Number类的对象调用各个方法并显 示计算结果。

public class Number {	private int n1=0;	private int n2=0;		public Number(int n1,int n2) {		this.n1= n1;		this.n2=n2;	}	//加	public int addition() {		int res = this.n1+this.n2;		return res;	}	//减	public int subtration() {		int res = this.n1-this.n2;		return res;	}	//乘	public int multication() {		int res = this.n1*this.n2;		return res;		}	//除	public int division() {		int res = this.n1/this.n2;		return res;			}	public static void main(String[] args) {		Number n = new Number(5, 6);		System.out.println(n.addition());		System.out.println(n.subtration());		System.out.println(n.multication());		System.out.println(n.division());	}}

5.编写Java程序用于显示人的姓名和年龄。定义一个人类Person该类中应该有两个私有属性姓名name和年龄 age。定义构造方法用来初始化数据成员。再定义显示display方法将姓名和年龄打印出来。 在main方法中创建人 类的实例然后将信息显示。

public class Person {	private String name;	private int age;	public Person(String name, int age) {		this.name = name;		this.age = age;	}	public void display() {		System.out.println("您的名字为:" + this.name);		System.out.println("您的年龄为:" + this.age);	}	public static void main(String[] args) {		Person p = new Person("李云龙", 35);		p.display();	}}

6.定义一个类该类有一个私有成员变量通过构造方法将其进行赋初值并提供该成员的getXXX()和setXXX()方法

public class Car {	private String name;	private int age;	public Car(String name, int age) {		this.name = name;		this.age = 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 static void main(String[] args) {		Car c = new Car("BWM", 12);		System.out.println(c.getAge());		c.setAge(18);				System.out.println(c.age);	}}

转载地址:http://egcki.baihongyu.com/

你可能感兴趣的文章
MediaCodec and Camera: colorspaces don't match
查看>>
android adb 读写模式 挂载文件系统
查看>>
onTouchEvent方法的使用
查看>>
Android详细解释键盘和鼠标事件
查看>>
迷茫的大学生(刘润,微软全球技术中心经理 )
查看>>
Linux自动运行程序五法
查看>>
让人敬佩的程序员
查看>>
WORD 域
查看>>
安装tomcat5时停在jvm.dll的解决
查看>>
Lucene 基础指南
查看>>
浏览器地址栏中加入ico图标
查看>>
Struts2文件的上传和下载
查看>>
Eclipse文件改变编码插件
查看>>
FreeMarker使用小结
查看>>
acegi-security-samples-contacts分析
查看>>
Acegi 设计概述
查看>>
让页面变得更快一点-HTML解析原理
查看>>
《谁在谋杀中国经济》与程序员
查看>>
The hierarchy of the type is inconsistent
查看>>
SpringSide 3 中的安全框架 Spring Security 2.0
查看>>