练习题目Day16

    技术2022-07-10  113

    package Day16;

    /* 2.请定义一个交通工具(Vehicle)的类 其中有属性: 速度(speed) 体积(size) 方法移动(move) 设置速度(setSpeed(int speed)) 加速speedUp() 减速speedDown()

    最后在测试类Vechicle中的main()中实例化一个交通工具对象 其通过方法对它初始化speed,size的值并打印出来 另外调用加速减速的方法对速度进行改变*/ public class Work01 { public static void main(String[] args) {

    //通过无参数构造方法创建对象 Vehicle v=new Vehicle (); v.setSpeed (0); v.setSize (5); System.out.println ("Speed:"+v.getSpeed ()); System.out.println ("Size:"+ v.getSize ()); //调用加速方法 v.speedUp(10); System.out.println ("Speed:"+v.getSpeed ()); //调用减速方法 v.speedDown (5); System.out.println ("Speed:"+v.getSpeed ()); //通过有参数构造方法创建对象 // Vehicle v1=new Vehicle (120,5); }

    } /交通工具/ class Vehicle{ // 速度 private int speed; // 体积 private int size;

    public Vehicle() { } public Vehicle(int speed,int size) { this.speed = speed; this.size=size; } /*设置速度的方法*/ /*实例方法的调用,需要先创建对象,通过引用来调用*/ public int getSpeed() { return speed; } public void setSpeed(int speed) { this.speed = speed; } public int getSize() { return size; } public void setSize(int size) { this.size = size; } //交通工具的移动方法 public void move(){ System.out.println ("交通工具:公共汽车:起步行驶。" ); } /*加速方法*/ public void speedUp(int addSpeed){ //在原来速度的基础上加 //this就是当前的交通工具对象 //安全判断 int oldSpeed=this.getSpeed ();//原来的速度 this.setSpeed (oldSpeed+addSpeed); } /*减速方法*/ public void speedDown(int subSpeed){ //在原本的基础上减去 //最好有判断 int oldSpeed=this.getSpeed (); this.setSpeed (oldSpeed-subSpeed); }

    }

    package Day16;

    /*4.编写Java程序模拟简单的计算器

    定义名为Number的类其中有两个整型数据成员n1和n2应声明为私有

    编写构造方法赋予n1和n2初始值

    再为该类定义加addition()、减subtration()、乘multplication()、除division()等公有实例方法

    分别对两个成员变量执行加、减、乘、除的运算

    在main方法中创建Number类的对象调用各个方法并显示计算结果*/ public class Work02 { public static void main(String[] args) { //创建对象 Number number=new Number ( 10,3 ); //计算 number.addtion (); number.subtration (); number.multplication (); number.division (); } } class Number{ private int n1; private int n2; public Number(){

    }

    public Number(int n1, int n2) { this.n1 = n1; this.n2 = n2; }

    public int getN1() { return n1; }

    public void setN1(int n1) { this.n1 = n1; }

    public int getN2() { return n2; }

    public void setN2(int n2) { this.n2 = n2; } //加 /可以返回值类型为void,也可以是int/ public void addtion(){

    //System.out.println (n1+"+"+n2+"="+(n1+n2)); System.out.println (this.getN1 ()+"+"+this.getN2 ()+"="+(this.getN1 ()+this.getN2 ()) );

    } //减 public void subtration(){

    int result=n1-n2; //int result=this.getN1 ()-this.getN2 (); System.out.println (n1+"-"+n2+"="+result);

    } //乘 public void multplication(){

    int result=n1*n2; System.out.println (n1+"*"+n2+"="+result);

    } //除 public void division(){

    int result=n1/n2; System.out.println (n1+"/"+n2+"="+result);

    } }

    package Day16;

    /*编写java程序用于显示人的姓名和年龄。

    定义一个人类Person该类中应该有两个私有属性姓名name和年龄age。

    定义构造方法用来初始化数据成员,再定义显示dispaly方法将姓名和年龄打印出来。

    再main方法中创建人类实例然后将信息显示*/ public class Work03 { public static void main(String[] args) { Person p1=new Person (); p1.setName (“张三”); p1.setAge (20); p1.display ();

    Person p2=new Person ("李四",30); p2.display ();

    } }

    class Person{

    public void display(){ //System.out.println ("姓名:"+this.getName ()+".年龄:"+this.getAge () ); System.out.println ("姓名:"+name+".年龄:"+age ); //System.out.println ("姓名:"+this.name+".年龄:"+this.age ); } private String name; private int age; public Person() { } public Person(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; }

    }

    package Day16;

    /在程序中经常需要对时间进行操作但是并没有时间类型的数据, 那么我们可以自己实现应该时间类来满足程序中的需要 定义名为MyTime的类其中应有三个整形成员时hour分minute秒second 为了保证数据的安全性这三个成员变量应声明私有 为MyTime类定义构造方法以方便创建对象时初始化成员变量 再定义display方法用于将时间信息打印出来 为MyTime类添加以下方法 addSecond(int sec) addMinute(int min) addHour(int hou) subSecond(int sec) subMinute(int min) subHour(int hou) 分别对时、分、秒进行加减运算/

    public class Work04 {

    public static void main(String[] args) { MyTime t1 = new MyTime ( ); t1.display (); MyTime t2=new MyTime (13,20,50); t2.display (); t2.addSecond (5); t2.display (); t2.addSecond (6); t2.display (); t2.addSecond (59); t2.display (); }

    } class MyTime{ private int hour; private int minute; private int second;

    public MyTime(int hour, int minute, int second) { this.hour = hour; this.minute = minute; this.second = second; } public MyTime() { } public int getHour() { return hour; } public void setHour(int hour) { this.hour = hour; } public int getMinute() { return minute; } public void setMinute(int minute) { this.minute = minute; } public int getSecond() { return second; } public void setSecond(int second) { this.second = second; } /*加秒*/ public void addSecond(int sec){ //this.setSecond (this.getSecond ()+sec); int oldSec=this.getSecond (); int newSec=oldSec+sec; if(newSec<60){ this.setSecond (newSec); }else if(newSec==60){ this.addMinute (1); this.setSecond (0); }else{ int newMinute=newSec/60; this.addMinute (newMinute); this.setSecond (newSec`); } } /*加分*/ public void addMinute(int min){ //this.setMinute (this.getMinute ()+min); int oldMinute=this.getMinute (); int newMinute=oldMinute+min; this.setMinute (newMinute); } /*加时*/ public void addHour(int hou){ this.setHour (this.getHour ()+hou); } /*减秒*/ public void subSecond(int sec){ this.setSecond (this.getSecond ()-sec); } /*减分*/ public void subMinute(int min){ this.setMinute (this.getMinute ()-min); } /*减时*/ public void subHour(int hou){ this.setHour (this.getHour ()-hou); } //set方法就是为了修改 public void display(){ //System.out.println (this.hour+ "时"+this.minute+"分"+this.second+"秒"); System.out.println (this.getHour ()+ "时"+this.getMinute ()+"分"+this.getSecond ()+"秒"); }

    }

    Processed: 0.014, SQL: 9