基础面对象
##练习题时钟#
package com.lovoinfo;
import java.util.Calendar;
/**
* 时钟
* @author zenghao
*
*/
public class Clock {
private int hour;
private int minute;
private int second;
/**
* 构造器
*/
public Clock() {
Calendar cal = Calendar.getInstance();
hour = cal.get(Calendar.HOUR_OF_DAY);
minute = cal.get(Calendar.MINUTE);
second = cal.get(Calendar.SECOND);
}
/**
* 构造器
* @param hour 时
* @param minute 分
* @param second 秒
*/
public Clock(int hour, int minute, int second) {
this.hour = hour;
this.minute = minute;
this.second = second;
}
/**
* 走字
*/
public void go() {
second += 1;
if(second == 60) {
second = 0;
minute += 1;
if(minute == 60) {
minute = 0;
hour += 1;
if(hour == 24) {
hour = 0;
}
}
}
}
/**
* 显示时间
* @return 返回当前时间
*/
public String display() {
String str = "";
if(hour < 10) {
str += "0";
}
str += hour + ":";
if(minute < 10) {
str += "0";
}
str += minute + ":";
if(second < 10) {
str += "0";
}
str += second;
return str;
}
/**
* 调整小时
* @param up true表示上调, false表示下调
*/
public void setHour(boolean up) {
if(up) {
hour = (hour + 1) % 24;
}
else {
hour -= 1;
if(hour == -1) {
hour = 23;
}
}
}
/**
* 调整分钟
* @param up true表示上调, false表示下调
*/
public void setMinute(boolean up) {
if(up) {
minute = (minute + 1) % 60;
}
else {
minute -= 1;
if(minute == -1) {
minute = 59;
}
}
}
/**
* 调整秒
* @param up true表示上调, false表示下调
*/
public void setSecond(boolean up) {
if(up) {
second = (second + 1) % 60;
}
else {
second -= 1;
if(second == -1) {
second = 59;
}
}
}
}
package com.lovoinfo;
import javax.swing.JOptionPane;
public class Test03 {
public static void main(String[] args) throws InterruptedException {
Clock c = new Clock();
JOptionPane.showMessageDialog(null, c.display());
System.exit(0);
}
}
手机
package com.lovoinfo;
/**
* 手机
* @author zenghao
*
*/
public class MobilePhone {
String brand;
double screenSize;
boolean smart;
/**
* 打电话
* @param telNumber 呼叫的号码
*/
public void call(String telNumber) {
System.out.println("正在呼叫" + telNumber);
}
/**
* 发短信
* @param telNumber 收信人
* @param message 消息的内容
*/
public void sendMsg(String telNumber, String message) {
System.out.println("向" + telNumber + "发送...");
System.out.println(message);
}
}
package com.lovoinfo;
public class Test01 {
public static void main(String[] args) {
MobilePhone phone = new MobilePhone();
phone.call("13812345678");
phone.sendMsg("13900998877", "晚上一起吃饭吧?");
}
}
学生
package com.lovoinfo;
/**
* 学生
* @author zenghao
*
*/
public class Student {
private String name;
private int age;
/**
* 构造器
* @param name 姓名
* @param age 年龄
*/
public Student(String name, int age) {
this.name = name;
this.age = age;
}
/**
* 吃饭
* @param food 吃的东西
*/
public void eat(String food) {
System.out.println(name + "正在吃" + food);
}
/**
* 睡觉
*/
public void sleep() {
System.out.println(name + "正在睡觉.");
}
/**
* 学习
* @param courseName 课程的名称
*/
public void study(String courseName) {
System.out.println(name + "正在学习" + courseName);
}
@Override
public String toString() {
return name + " - " + age;
}
}
package com.lovoinfo;
public class Test02 {
public static void main(String[] args) {
Student stu = new Student("王大锤", 20);
stu.eat("牛肉面");
stu.sleep();
stu.study("Java程序设计");
System.out.println(stu);
}
}
5.1作业:有一个游泳池我不知道它半径,我要修一个3M宽的人行道,和围墙。人行道修1平方要8元。围墙1米5元。总共要多少资费。
package com.lovoinfo;
public class Swim {
double radii;
public double Swin(double tariff) {
double R = radii + 3;
double money1 = ((R * R - radii * radii) * 3.14) * 8 + R * 3.14 * 5;
return money1;
}
}
package com.lovoinfo;
import java.util.Scanner;
public class Test001 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Swim circle = new Swim();
System.out.println("半径为:");
double Tariff = sc.nextDouble();
System.out.println("总共需要花费:" + circle.Swin(Tariff)+"元");
sc.close();
}
}