课堂练习1:
请定义一个交通工具(Vehicle)的类,其中有:属性:速度(speed),体积(size)等
方法:移动(move()),设置速度(setSpeed(int speed)),设置体积(setSize(int size))加速speedUp(),减速speedDown()等
在测试类Vehicle中的main()中实例化一个交通工具对象,通过方法给它初始化speed,size的值,并打印出来。另外,调用加速,减速的方法对速度进行改变。
具体代码:
public class Vehicle {
double speed;
double size;
public Vehicle(double speed,double size){
this.speed=speed;
this.size=size;
}
public void setSpeed(double speed) {
this.speed = speed;
System.out.println("速度修改成功"+speed);
}
public void setSize(double size) {
this.size = size;
System.out.println("体积修改成功"+size);
}
public void move(){
if(this.speed>0){
System.out.println("速度修改成功");
}
System.out.println("停车无法移动");
}
public void speedUp(){
if(this.speed<=99){
this.speed +=1;
System.out.println("加速成功,现在速度为:"+speed);
}
else{
System.out.println("速度达到最高,请注意安全");
}
}
public void speedDown(){
if(this.speed >=1){
this.speed-=1;
System.out.println("减速成功,现在的速度:"+speed);
}
else{
System.out.println("现在是停车,不能再进行减速");
}
}
}
public class VehicleTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Vehicle vehicle=new Vehicle(23, 100);
vehicle.setSize(120);
vehicle.setSpeed(0);
vehicle.speedUp();
vehicle.speedDown();
vehicle.move();
}
}
运行结果
课堂练习2:
打印当前时间。学习使用Date类和Calendar类。
import java.util.Calendar;
import java.util.Date;
public class NowTime {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Calendar calendar=Calendar.getInstance();
calendar.setTime(new Date());
int year=calendar.get(Calendar.YEAR);
int month=calendar.get(Calendar.MONTH)+1;
int day =calendar.get(Calendar.DAY_OF_MONTH);
int hour=calendar.get(Calendar.HOUR_OF_DAY);
int minute=calendar.get(Calendar.MINUTE);
int second=calendar.get(Calendar.SECOND);
System.out.println("使用Calender类打印现在时间:");
System.out.println(""+year+"年"+month+"月"+day+"日"
+hour+"时"+minute+"分"+second+"秒");
Date date=new Date();
System.out.println("使用Date打印日期:");
System.out.println("直接打印:当前时间为:"+date);
}
}
运行结果:
课堂练习3:
以Point类为基础,定义一个平面中的Circle类:
1、编写一个无参的构造函数;
2、编写一个有参的构造函数;
3、在主函数中调用无参的构造函数生成圆的实例c1,调用有参的构造函数生成圆的实例c2,调用实例方法判断c1和c2是否相重叠。
public class Point {
double x;
double y;
Point(){
super();
}
public double getX() {
return x;
}
public void setX(double d) {
this.x = d;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
}
public class Circle {
double r;
Point point;
Circle(){
//无参数的构造方法
super();
}
public Circle(int r, Point point) {
//有参数的构造方法
super();
this.r = r;
this.point = point;
}
public double getR() {
return r;
}
public void setR(double r) {
this.r = r;
}
public Point getPoint() {
return point;
}
public void setPoint(Point point) {
this.point = point;
}
}
import java.util.Scanner;
public class hanshuTest {
/**
* @param args
*/
static Circle circle1,circle2;
public static void main(String[] args) {
// TODO Auto-generated method stub
//手动输入圆1
circle1=new Circle();
InputData();
//构造方法产生圆2
circle2 = new Circle(3,new Point());
circle2.point.x=2;
circle2.point.y=3;
System.out.println("构建完成圆二。圆的具体位置:" +
"横座标"+circle2.getPoint().x+"纵座标"+circle2.getPoint().getY()+"半径"+circle2.r);
judgeTwoCircle();
}
private static void judgeTwoCircle() {
// 判断两个圆的位置
double i=Math.sqrt((circle1.getPoint().getX()-circle2.getPoint().getX())*(circle1.getPoint().getX()-circle2.getPoint().getX())+(circle1.getPoint().getY()-circle2.getPoint().getY())*(circle1.getPoint().getY()-circle2.getPoint().getY()));
if(i>circle1.getR()+circle2.getR()){
System.out.println("结果:两个圆相离");
}
else if(i==Math.abs(circle1.getR()+circle2.getR())){
System.out.println("结果:两个圆相切");
}
else if(i>Math.abs(circle1.getR()-circle2.getR())&&i<circle1.getR()+circle2.getR()){
System.out.println("结果:两个圆相交");
}
else if(i==Math.abs(circle1.getR()+circle2.getR())){
System.out.println("结果:两个圆内切");
}
else if(circle1.getPoint().getX()==circle2.getPoint().getX()&&
circle1.getPoint().getY()==circle2.getPoint().getY()&&
circle1.getR()==circle2.getR()){
System.out.println("结果:两圆重逢");
}
else{
System.out.println("错误");
}
}
private static void InputData() {
// TODO Auto-generated method stub
System.out.println("请输入座标点以及半径的值");
Scanner scanner=new Scanner(System.in);
circle1.setPoint(new Point());
circle1.point.setX(scanner.nextDouble());
circle1.point.setY(scanner.nextDouble());
circle1.setR(scanner.nextDouble());
System.out.println("输入完成圆一。圆的具体位置:" +
"横座标"+circle1.getPoint().x+"纵座标"+circle1.getPoint().getY()+"半径"+circle1.r);
}
}