Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1주차 Assignment - 박연지 #3

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions parkyeonji/first-java-assignment/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
### IntelliJ IDEA ###
out/
!**/src/main/**/out/
!**/src/test/**/out/

### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/

### VS Code ###
.vscode/

### Mac OS ###
.DS_Store
8 changes: 8 additions & 0 deletions parkyeonji/first-java-assignment/.idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions parkyeonji/first-java-assignment/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions parkyeonji/first-java-assignment/.idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions parkyeonji/first-java-assignment/first-java-assignment.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

87 changes: 87 additions & 0 deletions parkyeonji/first-java-assignment/src/assignments/Test1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package assignments;

// 다음 조건을 만족하도록 Cylinder 클래스를 작성합니다.
class Cylinder {

// 원주율 3.14를 정적 상수 PI로 선언과 동시에 초기화
static final double PI = 3.14;

// 정수형 원의 중심 좌표 x, y를 선언

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

클래스의 필드를 아래와 같이 private으로 선언하는 것이 객체 지향 프로그래밍의 중요한 원칙 중 하나인 캡슐화를 구현하기 위한 좋은 방법입니다. 참고해주세요!

private int x, y;
private double r;
private int height = 10;

int x, y;

// 실수형 원의 반지름 r을 선언
double r;

// 정수형 원기둥의 높이 height를 10으로 선언과 동시에 초기화
int height = 10;

// 생성자1: 정수 인자 x, y 와 실수 인자 r을 전달 받아서 해당 필드 값을 초기화
public Cylinder(int x, int y, double r) {
this.x = x;
this.y = y;
this.r = r;
}

// 생성자2: 정수 인자 x, y, height 와 실수 인자 r을 전달 받아서 해당 필드 값을 초기화
public Cylinder(int x, int y, double r, int height) {
this.x = x;
this.y = y;
this.height = height;
this.r = r;
}

// circleArea 메서드: PI를 이용하여 원의 면적 반환
double circleArea() {
return r * r * PI;
}

// volume 메서드: 면적과 높이를 이용하여 부피를 반환
double volume() {
return r * r * PI * height;
}

// surfaceArea 메서드: PI를 이용하여 원기둥의 겉넓이를 반환
double surfaceArea() {
return (r * r * PI * 2) + (2 * PI * r * height);
}

// move 메서드: 정수 인자 dx, dy를 전달 받아서 원의 중심 좌표를 이동
// - 예: 필드 x가 1이고 dx가 10이면 x는 11로 변경되어야 함
// 객체 자신을 반환
Cylinder move(int dx, int dy) {
this.x += dx;
this.y += dy;

return this;
}

void print() {
System.out.println("<"+x+","+y+":"+r+">");
}
}


// 메인메서드 수행시 이렇게 출력되도록
//<13,15:6.0>
//113.04
//1130.4
//602.88
//<17,25:10.0>
//314.0
//1570.0
//942.0
public class Test1 {
public static void main(String[] args) {
Cylinder c1 = new Cylinder(3,5, 6);
c1.move(10,10).print();
System.out.println(c1.circleArea());
System.out.println(c1.volume());
System.out.println(c1.surfaceArea());

Cylinder c2 = new Cylinder(5,8,10,5);
c2.move(12,17).print();
System.out.println(c2.circleArea());
System.out.println(c2.volume());
System.out.println(c2.surfaceArea());
}
}
11 changes: 11 additions & 0 deletions parkyeonji/first-java-assignment/src/assignments/assignments.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$" isTestSource="false" packagePrefix="assignments" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
29 changes: 29 additions & 0 deletions parkyeonji/first-java-assignment/src/bj_10810/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package bj_10810;
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

int N = scanner.nextInt();
int M = scanner.nextInt();

int i, j, k;

int array[] = new int[N];

for(int x=0;x<M;x++) {
i = scanner.nextInt();
j = scanner.nextInt();
k = scanner.nextInt();
Copy link

@shinheekim shinheekim Apr 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Scanner 잘 사용해주신거같은데 이 Scanner가 사용하기 쉽지만 대량의 데이터를 입력 받을 때 성능 문제가 생길 수 있습니다. 더 효율적인 코드를 위해서는 BufferedReader를 사용하는 것도 추천드립니다. 아래 블로그 참고해보시면 좋을 거같습니다~!

https://rlakuku-program.tistory.com/33


for(int l=i-1;l<j;l++) {
array[l] = k;
}
}

for(int y=0;y<N;y++) {
System.out.print(array[y] + " ");
}
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 20 additions & 0 deletions parkyeonji/first-java-assignment/src/bj_11021/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package bj_11021;
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

int T = scanner.nextInt();
int A,B;

for(int i=1;i<=T;i++) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

간단한 자바코드 작성시에는 System.out.println()를 반복적으로 사용해도 좋지만 결과를 StringBuilder에 추가해두었다가 마지막에 한번에 출력하는 방식도 사용해볼 수 있습니다.

StringBuilder sb = new StringBuilder();

        for(int i=1; i<=T; i++) {
            StringTokenizer st = new StringTokenizer(br.readLine());
            int A = Integer.parseInt(st.nextToken());
            int B = Integer.parseInt(st.nextToken());

            sb.append("Case #").append(i).append(": ").append(A+B).append("\n");
        }
            System.out.print(sb.toString());

A = scanner.nextInt();
B = scanner.nextInt();

System.out.println("Case #" + i + ": " + (A+B));
}

scanner.close();
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 22 additions & 0 deletions parkyeonji/first-java-assignment/src/bj_2439/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package bj_2439;
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

int num = scanner.nextInt();

for(int i=1;i<=num;i++) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

중첩 반복문을 활용하여 잘 구현해주셨습니다! 앞으로 코드 작성을 할 때 for(int i=1;i<=num;i++)이런식으로 붙여 사용하시는 것보다 가독성을 위해 for (int i = 1; i <= num; i++) 이런 식으로 작성하시는 것을 추천드립니다.

또한 별찍기 문제는 이것 외에도 많으니 반복문을 더 자세히 공부하고 싶으시다면 관련 문제를 더 풀어보시는 것도 좋을 거같습니다.

for(int j=num;j>i;j--) {
System.out.print(" ");
}
for(int k=0;k<i;k++) {
System.out.print("*");
}
System.out.println();
}

scanner.close();
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 17 additions & 0 deletions parkyeonji/first-java-assignment/src/bj_2588/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package bj_2588;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

int num1 = scanner.nextInt();
int num2 = scanner.nextInt();

System.out.println(num1 * (num2%10));
System.out.println(num1 * ((num2 % 100)/10));
System.out.println(num1 * (num2/100));
System.out.println(num1 * num2);

scanner.close();
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
31 changes: 31 additions & 0 deletions parkyeonji/first-java-assignment/src/bj_2884/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package bj_2884;
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

int H = scanner.nextInt();
int M = scanner.nextInt();

if(M<45) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저 if문을 조금 더 간결하게 작성할 수 있는 방법을 모색해보면 좋을 거같아요! 백준 문제를 꼭 혼자 풀어보시고나서 다른 사람들은 어떤 식으로 풀었는지 확인해보시면 추후 복잡한 코드를 보다 깔끔하게 작성하실 수 있을 거에요~!

스크린샷 2024-04-01 154126

if(H==0) {
H += 23;
M += 60;
M -= 45;
System.out.println(H + " " + M);
}
else {
H -= 1;
M += 60;
M -= 45;
System.out.println(H + " " + M);
}
}
else{
M -= 45;
System.out.println(H + " " + M);
}
scanner.close();
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 17 additions & 0 deletions parkyeonji/first-java-assignment/src/bj_9086/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package bj_9086;
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int T = scanner.nextInt();
scanner.nextLine();

for(int i=0;i<T;i++) {
String str = scanner.nextLine();
System.out.println(str.charAt(0) + "" + str.charAt(str.length()-1));
}

scanner.close();
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.