1、:
猜数字游戏:一个类A有一个成员变量v,有一个初值100。定义一个类,对A类的成员变量v进行猜。如果大了则提示大了,小了则提示小了。等于则提示猜测成功。
public class Question1{
int v=100;
Question1(int VB){
if (VB<v)
System.out.println("smaller");
else if (VB>v)
System.out.println("bigger");
else
System.out.println("right");
}
}
public class TestA{
public static void main(String args[]){
Question1 q = new Question1(30);
Question1 q1 = new Question1(120);
Question1 q2 = new Question1(100);;
}
}
2、
请定义一个交通工具(Vehicle)的类,其中有: 属性:速度(speed),体积(size)等等 方法:移动(move()),设置速度(setSpeed(int
speed)),加速speedUp(),减速speedDown()等等.
最后在测试类Vehicle中的main()中实例化一个交通工具对象,并通过构造方法给它初始化speed,size的值,并且通过打印出来。另外,调用加速,减速的方法对速度进行改变。
public class Vehicle{
double speed;
double size;
Vehicle(double speed, double size){
this.speed=speed;
this.size=size;
System.out.println("speed is "+speed);
System.out.println("size is "+size);
}
public void move(){
System.out.println("i am moving");
}
public void setSpeed(double speed){
this.speed=speed;
}
public void speedUp(){
speed = speed + 50;
System.out.println("the speed is turned up to: "+speed);
}
public void speedDown(){
speed = speed-20;
System.out.println("the speed is turned down to: "+speed);
}
public static void main(String args[]){
Vehicle v1 = new Vehicle(58.23,63.23);
v1.move();
v1.setSpeed(100);
v1.speedUp();
v1.speedDown();
}
}
4、 编写Java程序,模拟简单的计算器。
定义名为Number的类,其中有两个整型数据成员n1和n2,应声明为私有。编写构造方法,赋予n1和n2初始值,再为该类定义加(addition)、减(subtration)、乘(multiplication)、除(division)等公有成员方法,分别对两个成员变量执行加、减、乘、除的运算。 在main方法中创建Number类的对象,调用各个方法,并显示计算结果。
public class Number{
private double n1;
private double n2;
Number(double n1,double n2){
this.n1=n1;
this.n2=n2;
}
public void addition(){
double sum=0;
sum=n1+n2;
System.out.println("n1+n2 = "+sum);
}
public void subtration(){
double sum=0;
sum=n1-n2;
System.out.println("n1-n2 = "+sum);
}
public void multiplication(){
double sum=0;
sum=n1*n2;
System.out.println("n1 * n2 = "+sum);
}
public void division(){
System.out.println("n1 / n2 = "+n1/n2);
}
public static void main(String args[]){
Number number = new Number(500,100);
number.addition();
number.subtration();
number.multiplication();
number.division();
}
}
5: 编写Java程序,用于显示人的姓名和年龄。
定义一个人类(Person),该类中应该有两个私有属性,姓名(name)和年龄(age)。定义构造方法,用来初始化数据成员。再定义显示(display)方法,将姓名和年龄打印出来。
undefined
在main方法中创建人类的实例,然后将信息显示。
public class Person1{
private String name;
private int age;
Person1(String name, int age){
this.name=name;
this.age=age;
System.out.println("my name is: "+name);
System.out.println("my age is: "+age);
}
public static void main(String args[]){
Person1 p= new Person1("Sandy",35);
}
}
6、 为“无名粉店”写一个类:class WuMingFen 要求:
1.有三个属性:面码:String
theMa 粉的分量(两):int quantity 是否带汤:boolean likeSoup
2.写一个构造方法,以便于简化初始化过程,如: WuMingFen f1 = new
WuMingFen("牛肉",3,true);
3.重载构造方法,使得初始化过程可以多样化: WuMingFen f2 = new
WuMingFen("牛肉",2);
4.如何使得下列语句构造出来的粉对象是酸辣面码、2两、带汤的? WuMingFen f3 = new WuMingFen();
5.写一个普通方法:check(),用于查看粉是否符合要求。即:将对象的三个属性打印在控制台上。
public class WuMingFen{
String theMa;
int quantity;
boolean likeSoup;
WuMingFen(String theMa,int quantity){
this.theMa=theMa;
this.quantity=quantity;
}
WuMingFen(String theMa,int quantity,boolean likeSoup){
this(theMa,quantity);
this.likeSoup=likeSoup;
}
public void check(){
String y="Yes";
if (!likeSoup)
y="No";
System.out.println("theMa is: "+theMa);
System.out.println("The quantity is: "+quantity);
System.out.println("The likeSoup is: "+y);
}
public static void main(String args[]){
WuMingFen f1 = new WuMingFen("acid and spicy",2,true);
f1.check();
}
}