본문 바로가기
자바 프로그래밍/코딩

8-weeks

by 세인트킴 2023. 6. 7.

ArrayException

public class ArrayException{
	public static void main(String[] args){
		int[] intArray = new int[5];
		intArray[0] = 0;
		try {
			for(int i=0; i<5; i++) {
				intArray[i+1] = i+1 + intArray[i];
				System.out.println("intArray[" + i + "]" + "=" + intArray[i]);
			}
		}
		catch (ArrayIndexOutOfBoundsException e) {
			System.out.println("배열의 인덱스 범위를 벗어났습니다.");
		}
	}
}

Arraylength

import java.util.Scanner;

public class Arraylength{
	public static void main(String[] args){
		int intArray[] = new int[5];
		int sum = 0;
		
		Scanner scanner = new Scanner(System.in);
		System.out.print("Enter " + intArray.length + " integers >> ");
		for(int i=0; i<intArray.length; i++)
			intArray[i] = scanner.nextInt();
		
		for(int i=0; i<intArray.length; i++)
			sum += intArray[i];
		
		System.out.println("Average: " + (double)sum/intArray.length);
		scanner.close();
	}
}

Barchart

public class BarChart{
	public static void main(String[] args){
		int[] data = {5,2,18,9,6,11,4,5,13,7};
		
		for(int n:data) {
			System.out.printf("%2d ", n);
			for(int i=0; i<n; i++)
				System.out.print("*");
			System.out.println();
		}
	}
}

Calc

public class Calc{
	public static void main(String[] args){
		double sum=0;
		
		for(int i=0; i<args.length; i++)
			sum += Double.parseDouble(args[i]);
		
		System.out.println("sum = " + sum);
	}
}

DivideByZero

import java.util.Scanner;

public class DivideByZero{
	public static void main(String[] args){
		Scanner input = new Scanner(System.in);
		int dividend;
		int divisor;
		
		while(true) {
		System.out.print("나뉨수를 입력하시오: ");
		dividend = input.nextInt();
		System.out.print("나뉫수를 입력하시오: ");
		divisor = input.nextInt();
		try {
			System.out.println(dividend + "를 " + divisor + "로 나누면 몫은 " + dividend/divisor + "입니다.");
			break;
		}
		catch(ArithmeticException e) {
			System.out.println("0으로 나눌 수 없습니다! 다시 입력하세요: ");
		}
		input.close();
		}
	}
}

Foreach

public class Foreach{
	enum week {MONDAY, TUESDAY, WENDSDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY}
	
	public static void main(String[] args){
		int[] num = {1,2,3,4,5};
		int sum=0;
		for(int k:num)
			sum+=k;
		System.out.println("Sum " + sum);
		
		String names[] = {"apple", "pear", "banana", "cherry", "strawberry", "grapes"};
		
		for(String s:names)
			System.out.print(s + " ");
		System.out.println();
		
		for(week day:week.values())
			System.out.print(day + " ");
		System.out.println();
	}
}

InputException

import java.util.Scanner;
import java.util.InputMismatchException;

public class InputException{
	public static void main(String[] args){
		Scanner scanner = new Scanner(System.in);
		
		System.out.println("정수 3개를 입력하시오");
		
		int sum = 0, n = 0; 
		
		for(int i=0; i<3; i++) {
			System.out.print("[" + i + "] " + ">>");
			try {
				n = scanner.nextInt();
			}
			catch(InputMismatchException e) {
				System.out.println("정수가 아닙니다. 다시 입력하시오.");
				scanner.nextInt(); // 정수를 버리지 않고 토큰을 버린다.
				i--; // 인덱스가 증가하지 않도록 미리 감
				continue;
			}
			sum += n;
		}
		System.out.println("합은 " + sum);
		scanner.close();
	}
}

InstanceOfEx

class Person{}
class Student extends Person{}
class Researcher extends Person{}
class Professor extends Researcher{}

public class InstanceOfEx{
	static void print(Person p){
		if(p instanceof Person) System.out.print("Person ");
		if(p instanceof Student) System.out.print("Student ");
		if(p instanceof Researcher) System.out.print("Researcher ");
		if(p instanceof Professor) System.out.print("Professor ");
		System.out.println();
	}
	public static void main(String[] args){
		System.out.print("new Student() ->\t"); print(new Student());
		System.out.print("new Researcher() ->\t"); print(new Researcher());
		System.out.print("new Professor() ->\t"); print(new Professor());
	}
}

IrregularArray

public class irregularArray{
	public static void main(String[] args){
		int intArray[][] = new int[4][];
		intArray[0] = new int[3];
		intArray[1] = new int[2];
		intArray[2] = new int[3];
		intArray[3] = new int[2];
		
		for(int i=0; i<intArray.length; i++) 
			for(int j=0; j<intArray[i].length; j++) 
				intArray[i][j] = (i+1) * 10 + j;
		
		for(int i=0; i<intArray.length; i++) {
			for(int j=0; j<intArray[i].length; j++)
				System.out.print(intArray[i][j] + " ");
			System.out.println();
		}
	}
}

MethodOverridingEx

class Shape{
	public Shape next; 
	public Shape(){next = null;}
	public void draw(){
		System.out.println("Shape");
	}
}
class Line extends Shape{
	public void draw(){
		System.out.println("Line");
	}
}
class Rect extends Shape{
	public void draw(){
		System.out.println("Rect");
	}
}
class Circle extends Shape{
	public void draw(){
		System.out.println("Circle");
	}
}
public class MethodOverringEx{
	static void paint(Shape p){
		p.draw();
	}
	public static void main(String[] args){
		Line line = new Line();
		paint(line);
		paint(new Shape());
		paint(new Line());
		paint(new Rect());
		paint(new Circle());
	}
}

NumException

public class NumException{
	public static void main(String[] args){
		String[] stringNumber = {"23", "12", "3.141592", "998"};
		
		int i=0;
		try {
			for(i = 0; i<stringNumber.length; i++) {
				int j = Integer.parseInt(stringNumber[i]);
				System.out.println("숫자로 변환된 값은 " + j);
			}
		}
		catch(NumberFormatException e) {
			System.out.println(stringNumber[i] + "는 정수로 변환할 수 없습니다.");
		}
	}
}

ReturnArray

public class ReturnArray{
	static int[] makeArray() {
		int temp[] = new int[4];
		for(int i=0; i<temp.length; i++) 
			temp[i] = i;
		return temp;
		}
public static void main(String[] args){
	int intArray[];
	intArray = makeArray();
	for(int i=0; i<intArray.length; i++)
		System.out.print(intArray[i]+" ");
		}
	}

ScoreAverage

public class ScoreAverage{
	public static void main(String[] args){
		double score[][] = {{3.3, 3.4}, {3.5, 3.6}, {3.7, 4.0}, {4.1, 4.2}};
		double sum=0;
		for(int year=0; year<score.length; year++)
			for(int term=0; term<score[year].length; term++)
				sum += score[year][term];
		
		int n = score.length;
		int m = score[0].length;
		System.out.println("Average: " + sum/(n*m));
		}
	}

TestAverage

public class TestAverage{
	public static void main(String[] args){
		int score[][] = {{87, 96, 70},
						 {68, 87, 90},
						 {94, 100, 90},
						 {100, 81, 82},
						 {83, 65, 85},
						 {78, 87, 65},
						 {85, 75, 83},
						 {91, 94, 100},
						 {76, 72, 84},
						 {87, 93, 73}};
		
		for(int student=0; student<score.length; student++) {
			for(int test=0; test<score[student].length; test++)
				System.out.printf("%4d ", score[student][test]);
			System.out.printf("%.1f\n", getAverage(score[student]));
	}
		for(int test=0; test<score[0].length; test++)
			System.out.printf("%4.1f ", getTestAverage(score, test));
		System.out.println();
	}
	public static double getAverage(int[] arr){
		double m=0.0;
		for(int d:arr)
			m+=d;
		return m / arr.length;
	}
	public static double getTestAverage(int[][] arr, int test){
		double m=0.0;
		for(int student=0; student<arr.length; student++)
			m+=arr[student][test];
		return m / arr.length;
	}
}

'자바 프로그래밍 > 코딩' 카테고리의 다른 글

DateTest  (0) 2023.06.08
9-weeks  (0) 2023.06.07
Car.Ex.java  (0) 2023.06.02
QuadraticEquationSolver.java  (0) 2023.05.26
CurrencyConverter.java  (0) 2023.05.22