import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new java.util.Scanner(System.in); String name = input.nextLine(); int a = input.nextInt(); System.out.println("제 나이는" + a + "살입니다."); System.out.println("제 이름은" + name + "입니다."); System.out.println(sum(3, 4)); } public static int sum(int a, int b) { return a + b; } }
public class Main { public static void main(String[] args) { TV tv1 = new TV(); tv1.turnOn(); tv1.setChannel(30); tv1.setVolume(3); TV tv2 = new TV(5, 10); tv2.turnOn(); tv2.channelUp(); tv2.channelUp(); tv2.volumeUp(); System.out.println("tv1's channel is " + tv1.getChannel() + " and volume level is " + tv1.volumeLevel); System.out.println("tv2's channel is " + tv2.getChannel() + " and volume level is " + tv2.volumeLevel); } } class TV { private int channel; // Default channel is 1 int volumeLevel; // Default volume level is 1 boolean on; // By default TV is off public TV() { channel = 5; volumeLevel = 3; on = false; } public TV(int newChannel, int newVolume) { channel = newChannel; volumeLevel = newVolume; } public void turnOn() { on = true; } public void turnOff() { on = false; } public void setChannel(int newChannel) { if (on && newChannel >= 1 && newChannel <= 120) channel = newChannel; } public void setVolume(int newVolumeLevel) { if (on && newVolumeLevel >= 1 && newVolumeLevel <= 7) volumeLevel = newVolumeLevel; } public void channelUp() { if (on && channel < 120) channel++; } public void channelDown() { if (on && channel > 1) channel--; } public void volumeUp() { if (on && volumeLevel < 7) volumeLevel++; } public void volumeDown() { if (on && volumeLevel > 1) volumeLevel--; } public int getChannel() { return channel; } }
public class Man { private String name; private int age; public Man(String name, int age) { this.name = name; setAge(age); } public void tellYourInfo() { System.out.println("My name is " + name); System.out.println("Age is " + age); } public void setAge(int newAge) { if (newAge < 0) { newAge = 0; } age = newAge; } public String toString() { return name + ", " + age + "세"; } }
public class BusinessMan extends Man { private String company; private String position; public BusinessMan(String name, int age, String company, String position) { super(name, age); this.company = company; this.position = position; } public void tellYourInfo() { super.tellYourInfo(); tellYourBusiness(); } public void tellYourBusiness() { System.out.println("My company is " + company); System.out.println("My position is " + position); } public void business() { System.out.println("안녕하세요?"); } public String toString() { return super.toString() + " / " + company; } }
class Main { public static void main(String[] args) { Man[] people = {new Man("사람", -4), new Man("사람1", 5), new Man("사람2", 5), new BusinessMan("비지니스맨", 10, "중앙대", "잉여")}; for (int i = 0; i < people.length; i++) { System.out.println(people[i]); people[i].tellYourInfo(); } ((BusinessMan) people[3]).business(); } }
class PhoneInfo { String name; String phoneNumber; String birthday; ...
class Point implements Comparable<Point> Point(double x, double y) { this.x = x; this.y = y;} double x; double y; int compareTo(Object); String toString(); -> "(x, y)"꼴로 리턴 class Graph Point[] points = new Point[100]; void addPoint(Point); void printAll(); -> 모든 점 위치 출력 double getLength(); class Main 1. import java.util.Random Random random = new Random(); random.nextDouble() (0~1사이의 실수) 을 이용, 임의의 점 10개(좌표는 0~ 10)를 생성하고 printAll과 getLength 결과 출력 2. Math.sin(double)을 통해 0도, 5도, 10도, ... , 180도의 점을 추가, printAll과 getLength 결과 출력 (주의) 입력받는 값은 radian값이므로 Math.PI를 이용 90 / 180 * Math.PI = 90도 (hint) Math.pow(4, 0.5) => 2 Math.pow(3, 4) => 81 Arrays.sort(Obejct[]) for (Class objectName : arrayName) { }
package assignment; import java.util.Random; public class Main { public static void main(String[] args) { makePoints(); System.out.println("--------------------------------------------------------"); getCircleLength(); } public static void makePoints() { Random random = new Random(); Graph graph = new Graph(); for(int i = 0 ; i < 10 ; i ++ ) { graph.addPoint(new Point(10.0 * random.nextDouble(), 10.0 * random.nextDouble())); } System.out.println(graph.getLength()); graph.printAll(); } public static void getCircleLength() { Graph circleGraph = new Graph(181); for( int i = 0 ; i < 181 ; i ++) { Point graphPoint = new Point(Math.cos((double) i / 180.0 * Math.PI), Math.sin((double) i / 180.0 * Math.PI)); circleGraph.addPoint(graphPoint); } System.out.println(circleGraph.getLength()); circleGraph.printAll(); } }
package assignment; class Point implements Comparable<Point> { private double x; private double y; public Point () { this.x = 0; this.y = 0; } public double getX() { return x; } public double getY() { return y; } public Point (double x, double y) { this.x = x; this.y = y; } public String toString() { return ("(" + x + ", " + y + ")" ) ; } @Override public int compareTo(Point arg0) { if(x > arg0.x) return 1; else if (x == arg0.x) return 0; else return -1; } }
package assignment; import java.util.Arrays; public class Graph { private int end = 0; private double length = 0.0; public Point[] points; public Graph () { this(100); } public Graph (int leng) { points = new Point[leng]; } public void addPoint(Point p) { points[end] = new Point(p.getX(),p.getY()); end++; } public void printAll() { for(int i = 0 ; i < end ; i++) { System.out.println(points[i]); } } public double getLength() { Arrays.sort(points, 0, end); for (int i = 1 ; i < end ; i++) { length += Math.sqrt(Math.pow((points[i].getY() - points[i-1].getY()), 2) + Math.pow((points[i].getX() - points[i-1].getX()), 2)); } return length; } }
package practice; import java.util.ArrayList; import java.util.Random; public class Main { public static void main(String[] args) { ArrayList<Integer> list = new ArrayList<Integer>(); Random mola = new Random(); int i; for(i=0;i<1000;i++) list.add(i); list.remove(945); for(i=0;i<list.size();i++){ System.out.println(list.get(i)); } } }
package practice; public class MyStack<T> { private int index; private Object[] stack; public MyStack() { index = 0; stack = new Object[100]; } public void push(T o) { stack[index] = o; index++; } public T pop() { index--; return (T)stack[index]; } }
package practice; public class Person { public String name; public int age; public Person(int a, String b) { name = b; age = a; } public String toString(){ return "이름은"+name+"나이는"+age; } }