Java

2023.05.03

연을 2023. 5. 3. 17:25
728x90

CPU 제어점 ? 

getter/setter 빈즈라고도 불림.

 

1. 요구사항

[차의 스펙]

제작회사

모델

색깔

최고속도

[상태]

현재속도

[부품]

차체

엔진

타이어

 

2. 요구사항2

차를 초기화할때 모델, 컬러, 최고속도를 초기화하지 않거나

각각 초기화 하거나 전체를 초기화하거나 

초기화하고싶은 것을 선택해서 초기화하도록 하시오

 

3. 요구사항3

생성된 차를 운행하시오

 

1. Math.random() : Static을 썼으므로 Class소속
2. Math math = new Math(); : math라는 이름을 부여, new라는 함수를 썼으므로 객체 소속
    math.random();
: static을 붙이면 2번을 쓰지 못한다.
int strInt = String.Integer("123"); : 여기서 . of를 뜻한다 : ~의
String str = new String("안녕"); : new는 객체를 의미
1.번을 하게 되면 heap영역에 '현대'가 들어간다.
2.번으로 초기화를 하게 되면 저 선이 끊어지면서 1쪽에는 null값이 들어간다. 그 후에 heap에 들어있는 '현대'는 gc(garbage Collection)가 발생해 메모리가 반환된다.
  • 초기화 하는 순간 메모리를 잡는다.

[class diagram]

[코드]

[carSpecs]

package Car;

import java.awt.Color;

public class carSpecs {
	private String company;
	public String model;
	public Color color;
	public int topSpeed;
	public int currentSpeed;
	public String carBody;
	public String engine;
	public String tire;
	
	public String getCompany() {
		return company;
	}

	public void setCompany(String company) {
		this.company = company;
	}

	public String getModel() {
		return model;
	}

	public void setModel(String model) {
		this.model = model;
	}

	public Color getColor() {
		return color;
	}

	public void setColor(Color color) {
		this.color = color;
	}

	public int getTopSpeed() {
		return topSpeed;
	}

	public void setTopSpeed(int topSpeed) {
		this.topSpeed = topSpeed;
	}

	public int getCurrentSpeed() {
		return currentSpeed;
	}

	public void setCurrentSpeed(int currentSpeed) {
		this.currentSpeed = currentSpeed;
	}

	public String getCarBody() {
		return carBody;
	}

	public void setCarBody(String carBody) {
		this.carBody = carBody;
	}

	public String getEngine() {
		return engine;
	}

	public void setEngine(String engine) {
		this.engine = engine;
	}

	public String getTire() {
		return tire;
	}

	public void setTire(String tire) {
		this.tire = tire;
	}
	@Override
	public String toString() {
		return "-------------------"+"\n"
			+" MyCar Status"+"\n"
			+"----------------------"+"\n"
			+"\ncompany : \t"+this.company+"\n"
			+"model : \t"+this.model+"\n"
			+"color : \t"+this.color+"\n"
			+"topspeed : \t"+this.topSpeed+"\n"
			+"currentspeed : \t"+this.currentSpeed+"\n"
			+"carbody : \t"+this.carBody+"\n"
			+"engine : \t"+this.engine+"\n";
	}

	public carSpecs() {
		
	}
	public carSpecs(String model) {
		this.model=model;
	}
	public carSpecs(Color color) {
		this.color=color;
	}
	public carSpecs(int topspeed) {
		this.topSpeed=topspeed;
	}
	public carSpecs(String model, Color color) {
		this.model=model;
		this.color=color;
	}
	public carSpecs(String model, int topspeed) {
		this.model=model;
		this.topSpeed=topspeed;
	}
	public carSpecs(Color color,int topspeed) {
		this.color=color;
		this.topSpeed=topspeed;
	}
	public carSpecs(String model,Color color,int topspeed) {
		this.model=model;
		this.color=color;
		this.topSpeed=topspeed;
	}
	public void speedUp(int speed) throws InterruptedException {
		System.out.println("speed를 올라갑니다.");
		for(int i=0;i<speed;i++) {
			this.currentSpeed++;
			System.out.println(this.currentSpeed+"km/h");
			Thread.sleep(500);
		}
		
	}
	public void speedDown(int speed) throws InterruptedException {
		System.out.println("speed 내려갑니다");
		for(int i=speed;i>=0;i--) {
			this.currentSpeed--;
			System.out.println(this.currentSpeed+"km/h");
			Thread.sleep(500);
		}
		
	}
	public carSpecs(String company,String model, Color color, int topSpeed,int currentspeed,String carbody, String engine, String tire){
		this.company = company;
		this.model = model;
		this.color = color;
		this.topSpeed = topSpeed;
		this.currentSpeed = currentspeed;
		this.carBody = carbody;
		this.engine = engine;
		this.tire = tire;
	}

}

[Car]

package Car;

import java.awt.Color;
import java.util.Scanner;

public class Car {
	public static void main(String[] args) throws InterruptedException {
		Scanner s = new Scanner(System.in);
		carSpecs car1=new carSpecs("320d",Color.black,200);
		
		car1.setCompany("bmw");
		car1.setCarBody("f20");
		car1.setEngine("b47");
		car1.setTire("한국타이어");
		car1.setCurrentSpeed(0);
		
		System.out.println(car1.toString());
		
		car1.speedUp(20);
		//car1.speedDown(30);
	}

}

[결과]

-------------------
MyCar Status
----------------------
company : bmw
model : 320d
color : java.awt.Color[r=0,g=0,b=0]
topspeed : 200
currentspeed : 0
carbody : f20
engine : b47
speed�� �ö󰩴ϴ�.
1km/h
2km/h
3km/h
4km/h
5km/h
6km/h
7km/h
8km/h
9km/h
10km/h
11km/h
12km/h
13km/h
14km/h
15km/h
16km/h
17km/h
18km/h
19km/h
20km/h

구구단

[코드]

[gugudan]

package gugudan;

public class gugudan {
	private int gugudan;
	private int enddan;
	
	public int getGugudan() {
		return gugudan;
	}
	public void setGugudan(int gugudan) {
		this.gugudan = gugudan;
	}
	public int getEnddan() {
		return enddan;
	}
	public void setEnddan(int enddan) {
		this.enddan = enddan;
	}
	
	
	public gugudan() {
	}
	public void gugudan(int dan) {
		if(dan<=0) {
			System.out.println("Please enter from 0 or higher");
			return;
		}
		
		for(int j=1;j<10;j++) {
			System.out.println(dan+"x"+j+"="+(dan*j));
		}
		
	}
	
	public void everygugudan(int dan) {
		if(dan<=0) {
			System.out.println("Please enter from 0 or higher");
		}else {
			System.out.println("Multiplication tables up to "+dan);
			for(int i=1;i<=dan;i++) {
				gugudan(i);
				System.out.println();
			}
		}
	}
	
}

[gugu]

package gugudan;

import java.util.Scanner;

public class gugu {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		gugudan g = new gugudan();
		Scanner s = new Scanner(System.in);
		System.out.println("Please enter the column you want to see : ");
		int dan = s.nextInt();
		g.gugudan(dan);
		g.everygugudan(dan);

	}

}

[결과]

Please enter the column you want to see :
3
3x1=3
3x2=6
3x3=9
3x4=12
3x5=15
3x6=18
3x7=21
3x8=24
3x9=27

Multiplication tables up to 3

1x1=1
1x2=2
1x3=3
1x4=4
1x5=5
1x6=6
1x7=7
1x8=8
1x9=9

2x1=2
2x2=4
2x3=6
2x4=8
2x5=10
2x6=12
2x7=14
2x8=16
2x9=18

3x1=3
3x2=6
3x3=9
3x4=12
3x5=15
3x6=18
3x7=21
3x8=24
3x9=27

 

728x90