1、定义一个交通工具的类Vehicle:创建对象,get/set,if(), 构造方法,this.
/*请定义一个交通工具的类Vehicle
* 其中有属性:
* 速度(speed)
* 体积(size)等等
* 方法移动 move()
* 设置速度 setSpeed(int speed)
* 加速 speedUp()
* 减速 speedDoun()等等
* 在测试类Vehicle中的main()中实例化一个交通工具对象,并通过方法给她初始化speed,size的值并打印出来
* 另外调用加速减速的方法对速度进行改变*/
public class Test {
public static void main(String[] args) {
//创建交通工具对象
/*Vehicle vehicle = new Vehicle();
vehicle.setSpeed(0);
vehicle.setSize(5);*/
Vehicle vehicle = new Vehicle(0,5);
vehicle.move();
System.out.println("初始速度为:" + vehicle.getSpeed());
System.out.println("车身长度为:" + vehicle.getSize());
vehicle.speedUp(20);
vehicle.speedUp(40);
vehicle.speedUp(60);
System.out.println("speedUp:" + vehicle.getSpeed());
vehicle.speedDown(100);
System.out.println("speedDown:" + vehicle.getSpeed());
}
}
class Vehicle{
private int speed;
private int size;
public Vehicle(){}
public Vehicle(int speed,int size){
this.speed = speed;
this.size = size;
}
public void setSpeed(int speed){
this.speed = speed;
}
public int getSpeed(){
return speed;
}
public void setSize(int size){
this.size = size;
}
public int getSize(){
return size;
}
public void move(){
System.out.println("交通工具:公共汽车,起步行驶");;
}
public void speedUp(int addSpeed){
if(this.getSpeed() + addSpeed <= 80) {
this.setSpeed(this.getSpeed() + addSpeed);
}else{
this.setSpeed(80);
}
}
public void speedDown(int subSpeed){
if(this.getSpeed() >= subSpeed) {
this.setSpeed(this.getSpeed() - subSpeed);
}else{
this.setSpeed(0);
}
}
}
View Code
2、编写Java程序模拟简单的计算器:this.get/set,返回值类型,return
*编写Java程序模拟简单的计算器
定义名为Number的类,其中有两个整型数据成员n1和n2,声明为私有
编写构造方法赋予n1和n2初始值
再为该类定义加addition、减subtration、乘multiplication、除division等公有实例方法
分别对两个成员变量进行加减乘除的运算*/
public class Test {
public static void main(String[] args) {
Number number = new Number(10,0);
number.addition();
int sub = number.subtration();
System.out.println(number.getN1() + "-" + number.getN2() + "=" + sub);
number.multiplication();
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 addition(){
//int result = n1 + n2;
//System.out.println(n1 + "+" + n2 + "=" + result);
System.out.println(this.getN1() + "+" + this.getN2() + "=" + (this.getN1() + this.getN2()));
}
public int subtration(){
return n1 - n2;
}
public void multiplication(){
int result = n1 * n2;
System.out.println(this.getN1() + "*" + this.getN2() + "=" + result);
}
public void division(){
if(n2 == 0){
System.out.println("除数不能为0");
return;
}
int result = n1/n2;
System.out.println(this.getN1() + "+" + this.getN2() + "=" + result);
}
}
View Code
3、编写程序用于显示人的姓名和年龄:this.get/set
/*编写程序用于显示人的姓名和年龄
定义一个人类person该类中应该有两个私有属性姓名name和年龄age
定义构造方法用来初始化数据成员。再定义显示方法display()将姓名和年龄打印出来
在main方法中创建人类的实例然后将信息显示*/
public class Test {
public static void main(String[] args) {
Person person = new Person();
person.setName("张三");
person.setAge(20);
person.display();
Person person1 = new Person("李四",30);
person1.display();
Person person2 = new Person();
person2.display();
}
}
class Person{
public void display(){
System.out.println("姓名:" + this.getName() + ",年龄:" + this.getAge());
//System.out.println("姓名:" + this.name + ",年龄:" + this.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;
}
}
View Code
4、定义一个时间类,get/set,if(),创建对象,加减操作,加减运算
/*定义名为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 Test {
public static void main(String[] args) {
//创建时间的对象,初始时间,将信息输出
MyTime time1 = new MyTime();
time1.display();
MyTime time2 = new MyTime(23,55,50);
System.out.println("time2:");
time2.display();
//加减操作
time2.addSecond(5);
time2.display();
time2.addSecond(55);
time2.display();
time2.addSecond(5210);
time2.display();
}
}
class MyTime {
private int hour;
private int minute;
private int second;
public MyTime() {
}
public MyTime(int hour, int minute, int second) {
this.hour = hour;
this.minute = minute;
this.second = second;
}
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;
}
//加秒(秒的范围是0-59)
public void addSecond(int sec){
int newSec = this.getSecond() + sec;
if(newSec < 60) {
setSecond(newSec);
}else if(newSec >= 60){
int min = newSec / 60;
addMinute(min);
setSecond(newSec % 60);
}
}
//加分(分的范围是0-59)
public void addMinute(int min){
int newMin = this.getMinute() + min;
if(newMin < 60) {
setMinute(newMin);
}else if(newMin >= 60){
int hou = newMin / 60;
addHour(hou);
setMinute(newMin % 60);
}
}
//加时(时的范围是0-23)
public void addHour(int hou){
int newHou = this.getHour() + hou;
if(newHou < 24){
setHour(newHou);
}else if(newHou >= 24){
setHour(newHou % 24);
}
}
//减秒
public void subSecond(int sec){}
//减分
public void subMinute(int min){}
//减时
public void subHour(int hou){}
//打印
public void display() {
System.out.println(this.getHour() + "时" + this.getMinute() + "分" + this.getSecond() + "秒");
}
}
View Code
//即使再小的帆也能远航