수업시간 JAVA/문제
짝홀/배열 증가 감소시키기/ 변수 n에 값 넣기 등
SEOKIHOUSE
2023. 4. 8. 19:51
1) 짝홀 양수 음수
package news.practiceday1;
class Save {
int a;
public void set(int num) {
this.a = num;
}
public void notifys() {
if (a < 0) {
System.out.println("음수입니다" + a);
} else if (a > 0) {
System.out.println("양수입니다" + a);
} else if (a == 0) {
System.out.println("0입니다" + a);
}
}
public boolean isPositive() { // 양수체크
boolean result = false;
if (a > 0) {
result = true;
} else {
result = false;
}
return result;
}
public boolean negative() { // 양수체크
boolean result = false;
if (a < 0) {
result = true;
}
return result;
}
public boolean zakholss() { // 짝홀체크
boolean result = false;
if (a % 2 != 0) {
result = true;
}
return result;
}
public void zakHol() {
if (a % 2 !=0) {
System.out.println("홀수입니뎅" + a);
} else if (a % 2 == 0) {
System.out.println("짝수입니뎅" + a);
}
}
public void allinform() {
if (isPositive()) {
System.out.println("양수입니다");
} else if (negative()) {
System.out.println("음수입니다");
}
if (zakholss()) {
System.out.println("홀수입니다");
} else {
System.out.println("짝수입니다");
}
}
public boolean compare(Object obj) {
return true;
}
}
public class intzaruo {
public static void main(String[] args) {
// 정수자료 1개를 저장할수 있는 자료형을 만드쇼
// 양수, 음수, 0, 알려줘
// 짝수 홀수로 알려줘
Save sa = new Save();
sa.set(-6);
sa.notifys();
sa.zakHol();
boolean result = sa.isPositive();
System.out.println(sa.isPositive());
if (result) {
System.out.println("양수");
} else {
System.out.println("음수");
}
System.out.println("========");
result = sa.negative();
if (result) {
System.out.println("음수입니당");
} else {
System.out.println("양수입니다");
}
System.out.println("========");
result = sa.zakholss();
if (result) {
System.out.println("홀수");
} else {
System.out.println("짝쑤");
}
System.out.println("----------");
sa.allinform();
}
}
2) n에 값 넣고 그 값을 보려면 비번이 일치해야함
package news.practiceday1;
import java.util.Scanner;
class Sample {
int n;
int password;
public Sample(int n, int password) {
this.n = n;
this.password = password;
}
public void store(int see, int checkpw) {
if (see == 1 && password == checkpw) {
System.out.println(this.n);
} else if (password != checkpw) {
System.out.println("비밀번호가 틀렸습니다");
}
}
}
public class inputsample {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int go = 1;
while (go == 1) {
System.out.println("샘플 객체의 n 변수에 값을 부여하세요.");
System.out.print("값 입력 >>");
int gap = sc.nextInt();
System.out.print("pw를 설정하세요 >>");
int pw = sc.nextInt();
Sample sp = new Sample(gap, pw);
System.out.println("값이 저장되었습니다");
System.out.println("샘플 객체의 n 변수값을 보고 싶으신가요? (1:볼래 0:안볼래)");
int see = sc.nextInt();
if (see == 0) {
System.out.println("종료한다");
break;
}
System.out.print("패스워드를 입력하세요>>");
int checkpw = sc.nextInt();
sp.store(see, checkpw);
if(pw != checkpw) {
System.out.println("마지막 기회를 주겠다");
checkpw =sc.nextInt();
sp.store(see, checkpw);
if(pw != checkpw) {
System.out.println("마지막 기회를 날렸군 후후 수고하시게");
break;
}
}
System.out.print("종료하려면 0 아니면 1");
go = sc.nextInt();
}
}
}
3)배열 증가+감소 (arraylist배우기 전)
package news.practiceday1;
class array {
int[] a;
int index;
int[] b;
int[]c;
public array(int count) {
a = new int[count];
}
public void put(int value) {
if (index >= a.length) {
System.out.println("오류");
increase();
}
a[index] = value;
index++;
}
public void increase() {
b = new int[a.length + 1];
for (int i = 0; i < a.length; i++) {
b[i] = a[i];
}
a = b;
}
public void remove(int num) {
try {
a[num] = 0;
// for (int i = num - 1; i < a.length; i++) {
// if (i + 1 == a.length) {
// a[index - 1] = 0;
// } else {
// a[i] = a[i + 1];
// }
// }
// index--;
for (int i = num; i < a.length - 1; i++) { // 0 1 2 3 4
a[i] = a[i + 1];
}
a[index - 1] = 0;
index--; //index에 put해줬을때 맞는위치에 넣으려고 하는거다
//감소(decrease)만들어서 decrease(); 해도 된다
c = new int[a.length - 1];
for (int i = 0; i < a.length-1; i++) {
c[i] = a[i];
}
a = c;
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("삐빅 오류입니다!! 1~" + a.length + "숫자를 선택하세요");
}
}
public void print() {
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
}
}
public class Aclassactive5 {
public static void main(String[] args) {
array ar = new array(3);
ar.put(10);
ar.put(20);
ar.put(30);
ar.put(40);
ar.put(50);
ar.put(60);
ar.remove(0);
ar.remove(0);
ar.remove(0);
ar.print();
}
}
4)배열범위 넘어가면 값 알려주고 원하는 배열index값 가져오기
package news.practiceday1;
class MyArray {
int[] a;
int count;
MyArray() {
}
MyArray(int length) {
a = new int[length];
}
public void setA(int index, int num) {
if (index == a.length || index < 0) {
int arr = a.length - 1;
System.out.println("배열의 범위를 벗어났습니다 " + "0~" + arr + "의 값을 고르세요");
} else {
a[index] = num;
}
}
public int getA(int count) {
return a[count];
}
public void print() {
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
}
}
public class Aclassactive6 {
public static void main(String[] args) {
// 정수배열을 저장하는 자료형(1차원)
// 필요에 따라 크기를 정할수 있어야함
// 배열의 크기를 벗어나면 벗어났다고 알려줘야함
// 배열의 요소를 특정해서 볼수있으면 좋겠다
// 배열의 특정 인덱스에 자료를 넣을 수 있어야한다
// 배열의 모든 요소를 화면에 출력해야한다
MyArray ma1 = new MyArray(3);
ma1.setA(2, 20);
int a = ma1.getA(1);
System.out.println(a);
ma1.print();
}
}