코드비교

Old VersionNew Version
6private int speed = SLOW; 6private int speed = SLOW; 
7private boolean on = false; 7private boolean on = false; 
8private double radius = 5; 8private double radius = 5; 
9String color = "blue"; 9private String color = "blue";
10Fan1() { 10void setSpeed(int input) {
   11speed = input;
11} 12} 
12public String toString() { 12void setOn(boolean input) {
13if(on) { 13on = input;
14return String.format("speed %d\ncolor %s\nradius %.2f",speed,color,radius);   
15} 15} 
16else { 16void setRadius(double input) {
17return String.format("color %s\nradius %.2f\nfan is off",color,radius); 17radius = input;
18} 18} 
  19void setColor(String input) { 
  20color = input; 
19} 21} 
20int getSpeed() { 22int getSpeed() { 
21return speed; 23return speed; 
22} 24} 
23boolean isOn() { 23boolean getOn() {
24return on; 26return on; 
25} 27} 
26double getRadius() { 28double getRadius() { 
  
29String getColor() { 31String getColor() { 
30return color; 32return color; 
31} 33} 
32void setSpeed(int val) { 34Fan1() {}
33speed = val; 35public String toString() {
   36String result = "";
   37if(on) {
   38result += "speed " + speed + "\n" + "color " + color + "\n" + "radius " + String.format("%.2f", radius);
34} 39} 
35void setOn(boolean val) { 35else
36on = val; 36result += "color " + color + "\n" + "radius " + String.format("%.2f", radius) + "\n" + "fan is off";
37} 37return result;
38void setRadius(double val) {   
39radius = val;   
40}   
41void setColor(String val) {   
42color = val;   
43} 43} 
44} 44} 

sample code

import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
Fan1 fan1 = new Fan1();
for (int i = 0; i < n; i++) {
String op = sc.next();
String val = sc.next();
if (op.compareTo("speed") == 0) {
if (val.compareTo("SLOW") == 0) fan1.setSpeed(Fan1.SLOW);
else if (val.compareTo("FAST") == 0) fan1.setSpeed(Fan1.FAST);
else fan1.setSpeed(Fan1.MEDIUM);
} else if (op.compareTo("radius") == 0) {
fan1.setRadius(Double.parseDouble(val));
} else if (op.compareTo("color") == 0) {
fan1.setColor(val);
} else if (op.compareTo("on") == 0) {
if (val.compareTo("true") == 0) fan1.setOn(true);
else fan1.setOn(false);
}
}
System.out.println(fan1.toString());
}
}
class Fan1 {
final static int SLOW = 1;
final static int MEDIUM = 2;
final static int FAST = 3;
private int speed = SLOW;
private boolean on = false;
private double radius = 5;
String color = "blue";
Fan1() {
}
public String toString() {
if(on) {
return String.format("speed %d\ncolor %s\nradius %.2f",speed,color,radius);
}
else {
return String.format("color %s\nradius %.2f\nfan is off",color,radius);
}
}
int getSpeed() {
return speed;
}
boolean isOn() {
return on;
}
double getRadius() {
return radius;
}
String getColor() {
return color;
}
void setSpeed(int val) {
speed = val;
}
void setOn(boolean val) {
on = val;
}
void setRadius(double val) {
radius = val;
}
void setColor(String val) {
color = val;
}
}
class Fan1 {
final static int SLOW = 1;
final static int MEDIUM = 2;
final static int FAST = 3;
private int speed = SLOW;
private boolean on = false;
private double radius = 5;
private String color = "blue";
void setSpeed(int input) {
speed = input;
}
void setOn(boolean input) {
on = input;
}
void setRadius(double input) {
radius = input;
}
void setColor(String input) {
color = input;
}
int getSpeed() {
return speed;
}
boolean getOn() {
return on;
}
double getRadius() {
return radius;
}
String getColor() {
return color;
}
Fan1() {}
public String toString() {
String result = "";
if(on) {
result += "speed " + speed + "\n" + "color " + color + "\n" + "radius " + String.format("%.2f", radius);
}
else
result += "color " + color + "\n" + "radius " + String.format("%.2f", radius) + "\n" + "fan is off";
return result;
}
}