예제(12.13)

Dec 13, 2023
예제(12.13)
 

(가) 빛이 1년 동안 진행하는 거리를 계산하자

  • double형의 변수를 사용해보자
notion image
public class light { public static void main(String args[]){ final double LIGHT_SPEED =3e5; double distance; distance = LIGHT_SPEED * 365 * 24 * 60 * 60; System.out.println("빛이 1년동안 가는 거리 :" + distance + "km."); } }
실행(RUN): D:\tools\jdk-21.0.1\bin\java.exe "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2023.3\lib\idea_rt.jar=52155:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2023.3\bin" -Dfile.encoding=UTF-8 -Dsun.stdout.encoding=UTF-8 -Dsun.stderr.encoding=UTF-8 -classpath D:\workspace\java_lec\study\out\production\study ex02.light 빛이 1년동안 가는 거리 :9.4608E12km. Process finished with exit code 0

(나) 원의 면적 계산하기

  • 반지름이 5.0인 원의 면적을 계산하는 프로그램을 작성해보자. 모든 변수를 실수형으로 정의하고, 파이는 상수 PI로 정의하라.
notion image
public class AreaTest { public static void main(String args[]){ final double PI = 3.141592; double radius, area; radius = 5.0; area = PI * radius * radius; System.out.println("반지름이 5인 원의 면적은" + area); } }
 
실행(RUN): D:\tools\jdk-21.0.1\bin\java.exe "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2023.3\lib\idea_rt.jar=52162:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2023.3\bin" -Dfile.encoding=UTF-8 -Dsun.stdout.encoding=UTF-8 -Dsun.stderr.encoding=UTF-8 -classpath D:\workspace\java_lec\study\out\production\study ex02.AreaTest 반지름이 5인 원의 면적은78.5398 Process finished with exit code 0

(다) 형변환 실습하기

 
notion image
public class TypeConversion { public static void main(String args[]){ int i; double f; f = 1 / 5; System.out.println(f); // 소수점이 사라진다 > 0.0 f = (double) 1 / 5; System.out.println(f); // 소수점이 생긴다 > 0.2 i = (int) 1.7 + (int) 1.8; System.out.println(i); // 소수점이 사라지고 정수 1만 남아서 2가 된다 > 1+1=2 } }
실행(RUN): D:\tools\jdk-21.0.1\bin\java.exe "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2023.3\lib\idea_rt.jar=52164:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2023.3\bin" -Dfile.encoding=UTF-8 -Dsun.stdout.encoding=UTF-8 -Dsun.stderr.encoding=UTF-8 -classpath D:\workspace\java_lec\study\out\production\study ex02.TypeConversion 0.0 0.2 2 Process finished with exit code 0
 

(라) 사용자로부터 이름과 나이를 받는 프로그램

 
notion image
package ex02; import java.util.Scanner; public class InputString { public static void main(String[] args) { String name; int age; Scanner sc = new Scanner(System.in); System.out.println("이름을 입력하시오: "); name = sc.nextLine(); System.out.println("나이를 입력하시오: "); age = sc.nextInt(); System.out.println(name + "님 안녕하세요! " + (age) + "살이시네요"); } }
D:\tools\jdk-21.0.1\bin\java.exe "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2023.3\lib\idea_rt.jar=54791:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2023.3\bin" -Dfile.encoding=UTF-8 -Dsun.stdout.encoding=UTF-8 -Dsun.stderr.encoding=UTF-8 -classpath D:\workspace\java_lec\study\out\production\study ex02.InputString 이름을 입력하시오: 홍길동 나이를 입력하시오: 23 홍길동님 안녕하세요! 23살이시네요

(마) 관계 연산자 실습

 
notion image
notion image
 
package ex02; public class ComOperator { public static void main(String[] args) { System.out.println((3 == 4) + " "); System.out.println((3 != 4) + " "); System.out.println((3 > 4) + " "); System.out.println((3 < 4) + " "); System.out.println((3 == 3 && 4 == 7) + " "); System.out.println((3 == 3 || 4 == 7) + " "); } }
D:\tools\jdk-21.0.1\bin\java.exe "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2023.3\lib\idea_rt.jar=54830:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2023.3\bin" -Dfile.encoding=UTF-8 -Dsun.stdout.encoding=UTF-8 -Dsun.stderr.encoding=UTF-8 -classpath D:\workspace\java_lec\study\out\production\study ex02.ComOperator false true false true false true Process finished with exit code 0
 

(바) 피자

  • 20cm인 피자 2판과 30cm인 피자 1판 어느쪽이 이득인가?
notion image
notion image
package ex02; public class Pizza { public static void main(String[] args) { double area1 = 2 * 3.141592 * 20 * 20; double area2 = 3.141592 * 30 * 30; System.out.println("20cm 피자 면적=" + area1); System.out.println("30cm 피자 면적=" + area2); System.out.println((area1 > area2)? "20cm 두 개": "30cm 한 개"); } }
D:\tools\jdk-21.0.1\bin\java.exe "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2023.3\lib\idea_rt.jar=55000:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2023.3\bin" -Dfile.encoding=UTF-8 -Dsun.stdout.encoding=UTF-8 -Dsun.stderr.encoding=UTF-8 -classpath D:\workspace\java_lec\study\out\production\study ex02.Pizza 20cm 피자 면적=2513.2736 30cm 피자 면적=2827.4328 30cm 한 개 Process finished with exit code 0
 
Share article

RottenYogurt's Development Blog