Quiz) 커스텀클래스

🔓 임의의 값 입력받고 출려하기


메인클래스)

package pack_CusCls01;

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
				
		Scanner scanner = new Scanner(System.in);
		
		System.out.print("값을 입력하세요 : ");
		int num = scanner.nextInt();
		
		scanner.close();
		
		
		Sample sample = new Sample(num);
		sample.mtdPrint();
	}

}

커스텀클래스)

package pack_CusCls01;

public class Sample {
	
	private int num; //필드
	
	public Sample(int num) {
		this.num = num;
	} //생성자
	
	public void mtdPrint() {
		System.out.println("커스텀 클래스의 필드에 입력된 값 : " + num);
	}
}




🔓 급여 구하기


메인클래스)

package pack_CusCls02;

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		
		Scanner scanner = new Scanner(System.in);
		
		System.out.print("세전 급여를 입력해 주세요(단위. 원) : ");
		int money = scanner.nextInt();
		
		scanner.close();
		
		Salary salary = new Salary(money);
		
		salary.mtdCalc();
	}

}

커스텀클래스)

package pack_CusCls02;

public class Salary {

	private int money;

	public Salary(int money) {
		this.money = money;
	}
	
	public void mtdCalc() {
		double salary = (double)money-(money*0.033);
		System.out.printf("실지급액 : %.0f", salary);
	}
}




🔓 kg - pound 변환하기


메인클래스)

package pack_CusCls03;

import java.util.Scanner;

public class Conversion {

	public static void main(String[] args) {
		
		Scanner scanner = new Scanner(System.in);
		
		System.out.print("Kg을 입력해주세요 : ");
		int kilo = scanner.nextInt();
		
		scanner.close();
		
		Pound pound = new Pound(kilo);
		pound.mtdChange();
	}

}

커스텀클래스)

package pack_CusCls03;

public class Pound {

	private int kilo;

	public Pound(int kilo) {
		this.kilo = kilo;
	}
	
	public void mtdChange() {
		
		double lb = 2.2;
		double res = 0.0;
		res = this.kilo *lb;
		
		System.out.printf("파운드(lb) : %.1f lb", res);
	}
}




🔓✨ 숫자 입력받아 출력하기


메인클래스)

package pack_CusCls04;

import java.util.Scanner;

public class Shape {

	public static void main(String[] args) {
		
		Scanner scanner = new Scanner(System.in);
		
		System.out.print("숫자를 입력하세요 : ");
		int num = scanner.nextInt();
		
		if (num<3 || num>9) {
			System.exit(0);
		}
		
		scanner.close();
		
		NumRectangle numRec = new NumRectangle(num);
		
		numRec.mtdRect();
	}

}

커스텀클래스)

package pack_CusCls04;

public class NumRectangle {

	private int num;

	public NumRectangle(int num) {
		this.num = num;
	}
	
	public void mtdRect(){
		
		int k = 1;
		
		for (int i = 0; i < 10; i++) {
			
			for (int j = 0; j < 10; j++) {
				
				System.out.print(k++ + " ");
				if (k > num) k=1;
			}
				System.out.println();
		}
	}
}




🔓✨ 임의의 숫자 입력 받아 홀/짝수 누적 구하기


메인클래스)

package pack_CusCls05;

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		
		Scanner scanner = new Scanner(System.in);
		
		System.out.print("2개의 값을 입력하세요 : ");
		int num1 = scanner.nextInt();
		int num2 = scanner.nextInt();
		
		int start = num1;
		int end = num2;
		if (num1 > num2) {
			start = num2;
			end = num1;
		}
		
		System.out.print("누적할 숫자 : ");
		String chkString = scanner.next();
		int chkNum = 0; //짝수임을 가정
		if (chkString.equals("홀수")) chkNum = 1;
		
		scanner.close();
		
		Sum obj = new Sum(start, end, chkNum);
		int sum = obj.mtdSum();
		
		System.out.printf("%d부터 %d까지의 %s 누적 : %d"
				, start, end, chkString, sum);
	}

}

커스텀클래스)

package pack_CusCls05;

public class Sum {

	private int start;
	private int end;
	private int chkNum;
	
	public Sum(int start, int end, int chkNum) {
		this.start = start;
		this.end = end;
		this.chkNum = chkNum;
	}

	public int mtdSum() {
		int sum = 0;
		
		for (int i = start; i <= end; i++) {
			if(i % 2 == chkNum) sum += i;
		}
		
		return sum;
	}
}




🔓 입력한 숫자의 역순 출력하기


메인클래스)

package pack_CusCls06;

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		
		Scanner scanner = new Scanner(System.in);
		
		System.out.print("배열 크기 설정 : ");
		int len = scanner.nextInt();
		double[] num = new double[len];
		
		System.out.print("입력하실 숫자 4개를 입력해주세요 : ");
		for (int i = 0; i < num.length; i++) {
			num[i] = scanner.nextDouble();
		}
		
		scanner.close();
		
		ReverseArray reverseArray = new ReverseArray(num);
		reverseArray.mtdReverse();
	}

}

커스텀클래스)

package pack_CusCls06;

public class ReverseArray {
 
	private double[] num;

	public ReverseArray(double[] num) {
		this.num = num;
	}
	
	public void mtdReverse() {
		
		System.out.print("입력하신 숫자들의 역순 : ");
		
		for (int i = num.length-1; i >= 0; i--) {
			System.out.print(num[i] + " ");
		}
	}
}




🔓 입력한 숫자의 배수 구하기


메인메서드)

package pack_CusCls07;

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		
		Scanner scanner = new Scanner(System.in);
		
		System.out.print("배수를 구할 정수를 입력하세요 : ");
		int num = scanner.nextInt();
		
		if (num<2 || num>10) {
			System.out.println("올바른 값을 입력하세요."
          			  + "프로그램을 종료합니다.");
			System.exit(0);
			
		} else {
			Multiple multiple = new Multiple(num);
			multiple.mtd_Multiple();
		}
		
		scanner.close();
	}

}

커스텀클래스)

package pack_CusCls07;

public class Multiple {

	private int num;

	public Multiple(int num) {
		this.num = num;
	}
	
	public void mtd_Multiple() {
		
		for (int i = 2; i <= 50; i++) {
			if (i % num == 0) {
				System.out.print(i + " ");
			}
		}
	}
}

++) 한 줄에 10개씩 출력하기

	public void mtd_Multiple() {
		
		int cnt = 0;
		
		for (int i = 2; i <= 50; i++) {
			
			if (i % num == 0) {
				System.out.print(i + " ");
				cnt++;
			}	if (cnt % 10 == 0) {
				System.out.println();
			}
		}
	}




🔓✨ 평균 경제 성장률과 평균 초과 횟수 구하기


메인클래스)

package pack_CusCls08;

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		
		Scanner scanner = new Scanner(System.in);
		
		System.out.print("10년간의 경제성장률을 입력하세요 : ");
		double[] growthRate = new double[10];
		for (int i = 0; i < growthRate.length; i++) {
			growthRate[i] = scanner.nextDouble(); 
		}
		
		scanner.close();
		
		Analysis analysis = new Analysis(growthRate);
		
		analysis.mtd_Avg();
		analysis.mtd_Cnt();
	}

}

커스텀클래스)

package pack_CusCls08;

public class Analysis {

	private double[] growthRate;
	private double avg;

	public Analysis(double[] growthRate) {
		this.growthRate = growthRate;
	}
	
	
	public void mtd_Avg() {
		double sum = 0.0;
		
		for (int i = 0; i < growthRate.length; i++) {
			sum += growthRate[i];
		}
	
		avg = sum / growthRate.length;
		System.out.printf("평균 경제성장률 : %.2f", avg);
	}
	

	public void mtd_Cnt() {
		int cnt = 0;
		
		for (int i = 0; i < growthRate.length; i++) {
			
			if (growthRate[i] > this.avg) {
				cnt++;
			}
		}
		System.out.println("\n평균 경제성장률을 초과한 횟수 : " 
        						+ cnt);
	}
}

좋은 웹페이지 즐겨찾기