Exercise/Java/Data Abstraction

From YuntechWiki

Jump to: navigation, search

example

將lesson 12的第2題改成存hours
public class Attraction {
private double hours;
//定義無參數constructor,hours設為1.25
//定義constructor傳入一個double h, hours設為m
//定義getMinutes,傳回hours*60之值(int)
//定義getHours
//定義setMinutes,傳入int m,設定hours為m除以60之值(double)
//定義setHours
}
class ex01{
public static void main(String args[]){
//create Attraction之instance att1
(使用無參數constructor)
//印出att1的minutes
//印出att1的hours
//create Attraction之instance att2
(使用一個參數的constructor, 3.75)
//印出att2的minutes
//印出att2的hours
//設定att2的minutes為315
//印出att2的minutes
//印出att2的hours
//設定att2的hours為2.5
//印出att2的minutes
//印出att2的hours
}
}
 
 
[解答]
 
 
class Attraction {
private double hours;
Attraction () {
hours = 1.25;
}
Attraction (double h) {
hours = h;
}
int getMinutes () {
return (int)(hours*60);
}
double getHours () {
return hours;
}
void setMinutes (int m) {
hours = m/60.0;
}
void setHours (double h) {
hours = h;
}
}
class ex01{
public static void main(String args[]){
Attraction att1 = new Attraction();
System.out.println(att1.getMinutes());
System.out.println(att1.getHours());
Attraction att2 = new Attraction(3.75);
System.out.println(att2.getMinutes());
System.out.println(att2.getHours());
att2.setMinutes(315);
System.out.println(att2.getMinutes());
System.out.println(att2.getHours());
att2.setHours(2.5);
System.out.println(att2.getMinutes());
System.out.println(att2.getHours());
}
}
 
 
 
[2]
將上一題的Attraction class
加入getSeconds, setSeconds, getDays, setDays, getWeeks, setWeeks
public class Attraction {
private int seconds;
//定義constructor傳入一個int s, seconds設為s
//定義getSeconds(int)
//定義getMinutes(double)
//定義getHours(double)
//定義getDays(double)
//定義getWeeks(double)
//定義setSeconds(int)
//定義setMinutes(double)
//定義setHours(double)
//定義setDays(double)
//定義setWeeks(double)
}
class ex02{
public static void main(String args[]){
//create Attraction之instance att1 (傳入58620)
//印出att1的seconds
//印出att1的minutes
//印出att1的hours
//印出att1的Days
//印出att1的Weeks
//設定att1的Weeks(傳入2.5)
//印出att1的seconds
//印出att1的minutes
//印出att1的hours
//印出att1的Days
//印出att1的Weeks
}
}
 
 
[解答]
 
 
class Attraction {
private int seconds;
Attraction (int s) {
seconds = s;
}
int getSeconds(){
return seconds;
}
double getMinutes () {
return seconds/60.0;
}
double getHours () {
return getMinutes()/60;
}
double getDays () {
return getHours()/24;
}
double getWeeks () {
return getDays()/7;
}
void setSeconds (int s) {
seconds = s;
}
void setMinutes (double m) {
setSeconds((int)(m*60));
}
void setHours (double h) {
setMinutes(h*60);
}
void setDays (double d) {
setHours(d*24);
}
void setWeeks (double w) {
setDays(w*7);
}
}
class ex02{
public static void main(String args[]){
Attraction att1 = new Attraction(58620);
System.out.println(att1.getSeconds());
System.out.println(att1.getMinutes());
System.out.println(att1.getHours());
System.out.println(att1.getDays());
System.out.println(att1.getWeeks());
att1.setWeeks(2.5);
System.out.println(att1.getSeconds());
System.out.println(att1.getMinutes());
System.out.println(att1.getHours());
System.out.println(att1.getDays());
System.out.println(att1.getWeeks());
}
}
Personal tools