[TIL] 유데미 X 사람인 취업 부트캠프 19일차 / java
[1. 오늘 배운 것]
1. 연습문제2
- 인치를 객체로
public class Dimension {
private int feet;
private int inches;
public Dimension(int inches) {
if (inches < 0) {
this.feet = -1;
this.inches = -1;
} else {
this.feet = inches / 12;
this.inches = inches % 12;
}
}
public int getFeet() {
return feet;
}
public int getInches() {
return inches;
}
}
- 정사각형 클래스
public class Square {
private int side;
public Square(int side) {
this.side = side;
}
public int calculateArea() {
if (side <= 0) {
return -1;
}
return side * side;
}
public int calculatePerimeter() {
if (side <= 0) {
return -1;
}
return side * 4;
}
}
- 2차원 좌표 x, y가 있는 point 클래스 만들기
// Defining a Point class to represent a point in 2-dimensional space
public class Point {
private int x;
private int y;
public Point(int x, int y) {
this.x = x; // Assigning x-coordinate of the point
this.y = y; // Assigning y-coordinate of the point
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public void move(int dx, int dy) {
this.x = x + dx;
this.y = y + dy;
}
public double distanceTo(Point other) {
int diffx = this.x - other.x;
int diffy = this.y - other.y;
return Math.sqrt(diffx * diffx + diffy * diffy);
}
}
- RGB Color
public class RGBColor {
private int red;
private int green;
private int blue;
public RGBColor(int red, int green, int blue) {
this.red = red;
this.green = green;
this.blue = blue;
}
public int getRed() {
return this.red;
}
public int getGreen() {
return this.green;
}
public int getBlue() {
return this.blue;
}
public void invert() {
this.red = 255 - this.red;
this.green = 255 - this.green;
this.blue = 255 - this.blue;
}
}
2. java 자료형의 기초
- jshell에서 각 자료형의 가장 큰 값, 작은 값, 사이즈, 비트수를 알 수 있다
3. Post i, Pre i
- 증감연산자에서, ++i와 i++은 다르다
- j = i++에서, i의 값이 증가하기 전 값이 입력된다
- k = ++i에서, i의 값이 증가한 후의 값이 입력된다
4. 자료형 기초 연습문제
- BiNumberRunner
package Integer;
public class BiNumberRunner {
public static void main(String[] args) {
BiNumber numbers = new BiNumber(2, 3);
System.out.println(numbers.add());
System.out.println(numbers.multiply());
numbers.doubleValue();
System.out.println(numbers.getNumber1());
System.out.println(numbers.getNumber2());
}
}
- BiNumber
package Integer;
public class BiNumber {
private int number1;
private int number2;
public int getNumber1() {
return number1;
}
public void setNumber1(int number1) {
this.number1 = number1;
}
public int getNumber2() {
return number2;
}
public void setNumber2(int number2) {
this.number2 = number2;
}
public BiNumber(int number1, int number2) {
this.number1 = number1;
this.number2 = number2;
}
public int add() {
return number1 + number2;
}
public int multiply() {
return number1 * number2;
}
public void doubleValue() {
this.number1 *= 2;
this.number2 *= 2;
}
}
- 결과
5. 부동소수점 자료형
- float형을 선언할 때에는 f또는 F를 붙여줘야 한다
- 작은 값을 큰 값으로 변환할 수 없음
- explicit 형변환 필요
6. BigDecimal
- 부동소수는 소숫점값을 명료하게 나타내지 못함
- 결과의 정확성이 중요한 것에는 float이나 double을 사용하기보다, BigDecimal을 사용하는 것이 좋음
- 단점 : 값을 한 번 설정하면 바꿀 수 없음
- number1.을 입력한 후 tab을 누르면 다양한 연산을 볼 수 있다
- BigDecimal과 다른 타입의 수 더하기
- 다른 타입의 수를 더하려면, new BigDecimal을 사용해서 더해야 한다
7. BigDecimal 연습문제
- SimpleInterestCalculator
package BigDecimal;
import java.math.BigDecimal;
public class SimpleInterestCalculator {
BigDecimal principal;
BigDecimal interest;
public SimpleInterestCalculator(String principal, String interest){
this.principal = new BigDecimal(principal);
this.interest = new BigDecimal(interest).divide(new BigDecimal(100));
}
public BigDecimal calculateTotalValue(int noOfYears){
BigDecimal noOfYearsBigDecimal = new BigDecimal(noOfYears);
BigDecimal totalValue = principal
.add(principal.multiply(interest)
.multiply(new BigDecimal(noOfYears)));
return totalValue;
}
}
- SimpleInterestCalcualtorRunner
package BigDecimal;
import java.math.BigDecimal;
public class SimpleInterestCalculatorRunner {
public static void main(String[] args) {
SimpleInterestCalculator calculator = new SimpleInterestCalculator("4500.00", "7.5");
BigDecimal totalValue = calculator.calculateTotalValue(5);
System.out.println(totalValue);
}
}
- 결과
8. 관계연산과 논리연산
- Boolean 자료형 : true 또는 false의 값을 갖는 자료형
- 관계연산자 : >, <, <=, >=
- 논리연산자 : &&(둘 다 참이어야 참 반환), ||(둘 중 하나만 참이어도 참 반환), ^(두 개의 값이 달라야 참 반환), !(값이 거짓이면 참 반환)
9. char 연습문제1
- MyCharRunner
package Char;
public class MyCharRunner {
public static void main(String[] args) {
MyChar myChar = new MyChar('c');
System.out.println(myChar.isVowel());
//MyChar.printLowerCaseAlphbets();
//MyChar.printLowerCaseAlphbets();
}
}
- MyChar
package Char;
public class MyChar {
private char ch;
public MyChar(char ch){
this.ch = ch;
}
public boolean isVowel(){
if (ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')
return true;
if (ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')
return true;
return false;
}
}
10. if-else문
- if (조건) {조건이 참일 경우 실행될 명령문}
- else if (조건2) {if문에 해당되지 않지만 조건2가 참일 경우 실행될 명령문}
- else {조건과 조건 2 모두 거짓일 때 실행될 명령문}
11. switch문
- case별로 그 아래 명령문들까지 실행
- case하나만 실행하고 싶을 때에는 break활용
12. 삼항연산자
- 기본 구조 : (연산) ? (참일 때 반환값) : (거짓일 때 반환값)
[2. 오늘 잘한 점]
많이 안졸고 해야 할 일을 다 끝낼 수 있었다! 팀스터디도 원만히 끝내서 뿌듯한 하루였다.
[3. 개선해야할 점]
노트북 거치대랑 마우스랑 키보드를 사와야겠다 불편해 죽겠슴