archive
[6주차 실습] 4월 6일 Problem Set 본문
1.The following code is the main method we want to run, and the following result is the corresponding console output. Declare the Rectangle class for this main method.
package April06;
public class Project01 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Rectangle r=new Rectangle(2,2,8,7);
Rectangle s=new Rectangle(5,5,6,6);
Rectangle t=new Rectangle(1,1,10,10);
r.show();
System.out.println("Area of s is "+s.square());
if(t.contains(r))System.out.println("t contains r.");
if(t.contains(s))System.out.println("t contains s.");
}
}
package April06;
public class Rectangle {
private int width, height;
private int xpos, ypos;
Rectangle(int xpos, int ypos, int width, int height){
this.width=width;
this.height=height;
this.xpos=xpos;
this.ypos=ypos;
}
void show(){
System.out.printf("A rectangle with %dx%d at (%d, %d)\n", this.width, this.height, this.xpos, this.ypos);
}
int square() {
return (width*height);
}
boolean contains(Rectangle r) {
if(this.xpos < r.xpos && this.ypos < r.ypos)
if((this.width+this.xpos) > (r.xpos+r.width) && (this.height+this.ypos) > (r.ypos+r.height))
return true;
return false;
}
}
<실행결과>
2. The class Day, which represents a day’s to-do list, is as follows. Write a class MonthSchedule to represent a month’s to-do. Create an array of Day objects, a constructor, and the input(), view(), finish(), and run() methods in the MonthSchedule class, and the main function performs only these two commands.
• MonthSchedule april = new MonthSchedule(30);
• april.run();
package April06;
public class Project02 {
public static void main(String[] args) {
// TODO Auto-generated method stub
MonthSchedule april=new MonthSchedule(30);
april.run();
}
}
package April06;
public class Day {
String work;
void set(String work) {
this.work=work;
}
String get() {
return work;
}
void show() {
if(work==null)
System.out.println("Nothing.");
else
System.out.print("There is ["+work+"]");
}
}
package April06;
import java.util.*;
public class MonthSchedule {
Scanner sc=new Scanner(System.in);
Day date[];
void input(){
System.out.print("Date(1-30)? ");
int day=sc.nextInt();
System.out.print("Do list (without space)?");
String stuff=sc.next();
day--;
date[day].set(stuff);
}
void view() {
System.out.print("Date(1-30)? ");
int day=sc.nextInt();
day--;
date[day].show();
System.out.println("on the "+(day+1)+"th.");
}
void finish() {
System.out.print("This is the end of the program.");
}
void run() {
System.out.println("This is a schedule management program for this month.");
while(true) {
System.out.print("Command (Insert: 1, Show: 2, Quit: 3) >>>");
int num=sc.nextInt();
if(num==1) {
input();
}
else if(num==2) {
view();
}
else {
finish();
break;
}
}
}
MonthSchedule(int day) {
this.date = new Day[day];
for(int i=0; i<date.length; i++) {
date[i] = new Day();
}
}
}
<실행결과>
'Java > 23_1 소프트웨어프로젝트' 카테고리의 다른 글
[9주차 실습] 4월 28일 Problem Set (0) | 2023.04.28 |
---|---|
[7주차 실습] 4월 13일 Problem Set (0) | 2023.04.28 |
[5주차 실습] 3월 30일 Problem Set (0) | 2023.03.31 |
[4주차 실습] 3월 23일 Problem Set (0) | 2023.03.30 |
[3주차 실습] 3월 14일 Problem Set (0) | 2023.03.18 |