본문 바로가기
학교/컴프1

컴프 1 실습 코드 정리 (W9~W10)

by 발빠진 쥐 2024. 5. 26.

Week9

CalculatorTest

package Week9;

import java.util.Scanner;

public class CalculatorTest {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		Calculator i = new Calculator();
		
		int value1 = i.add();
		int value2 = i.subtract();
		int value3 = i.multiply();
		int value4 = i.divide();
		
		System.out.println(value1);
		System.out.println(value2);
		System.out.println(value3);
		System.out.println(value4);
		
	}

}

class Calculator {
	Scanner sc = new Scanner(System.in);
	
	int a = sc.nextInt() ;
	int b = sc.nextInt() ;
	
	 void setOperand(int aa, int bb) { 
	       aa = a;
	       bb = b;
	    }
	
	int add() {
		return a+b;
	}
	
	int subtract() {
		return a-b;
	}
	
	int multiply() {
		return a*b;
	}
	
	int divide() {
		return a/b;
	}
	
}

 

 

SortingTest

package Week9;

import java.util.Arrays;
import java.util.Scanner;

public class SortingTest {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);

		int[] arr = new int[10];

		for (int i = 0; i < 10; i++) {
			arr[i] = sc.nextInt();
		}

		Sorting a = new Sorting();

		arr = a.sort(arr);
		
		for (int j = 0; j < 10; j++) {
			System.out.print(arr[j] + " ");
		}

	}

}

class Sorting {
	int[] sort;

	int[] sort(int[] arr) {
		Arrays.sort(arr); 
		return arr;
	}
}

 

 

Week10

OperatorTest

package Week10;


import java.util.Scanner;

public class OperatorTest {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);

		int a = sc.nextInt();
		int b = sc.nextInt();
		String y = sc.next();

		int value = 0;

		if (y.equals("+")) {
			Add i = new Add();
			i.setOperand(a, b);
			value = i.calculate();
		}

		else if (y.equals("-")) {
			Sub j = new Sub();
			j.setOperand(a, b);
			value = j.calculate();
		}

		else if (y.equals("*")) {
			Mul l = new Mul();
			l.setOperand(a, b);
			value = l.calculate();
		}

		else {
			Div k = new Div();
			k.setOperand(a, b);
			value = k.calculate();

		}
		
		System.out.println(value);

	}

}

class Add {

	int a;
	int b;

	void setOperand(int a, int b) {
		this.a = a;
		this.b = b;
	}

	int calculate() {
		return a + b;
	}

}

class Sub {
	int a;
	int b;

	void setOperand(int a, int b) {
		this.a = a;
		this.b = b;
	}

	int calculate() {
		return a - b;
	}

}

class Mul {
	int a;
	int b;

	void setOperand(int a, int b) {
		this.a = a;
		this.b = b;
	}

	int calculate() {
		return a * b;
	}

}

class Div {
	int a;
	int b;

	void setOperand(int a, int b) {
		this.a = a;
		this.b = b;

	}

	int calculate() {
		return a / b;
	}

}

 

TriangleTest

package Week10;

import java.util.Scanner;

public class TriangleTest {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);

		double width1 = sc.nextDouble();
		double height1 = sc.nextDouble();

		double width2 = sc.nextDouble();
		double height2 = sc.nextDouble();

		Triangle semo1 = new Triangle(width1, height1);
		Triangle semo2 = new Triangle(width2, height2);

		double area1 = semo1.area();
		double area2 = semo2.area();

		System.out.println(area1);
		System.out.println(area2);
		System.out.println(semo1.isSameArea(semo2));
	}

}

class Triangle {

	double width;
	double height;

	void setOperand(double width, double height) {
		this.width = width;
		this.height = height;
	}

	public Triangle(double width, double height) {
		this.width = width;
		this.height = height;
	}

	double area() {
		return (width * height) / 2;
	}

	boolean isSameArea(Triangle t) {
		return this.area() == t.area(); 
	}

}

'학교 > 컴프1' 카테고리의 다른 글

컴프 실습 코드 정리 (Week12)  (1) 2024.06.11
컴프 1 실습 코드 정리 (Week11)  (0) 2024.05.26
컴프 1 실습 코드 정리(W7)  (0) 2024.05.26
컴프 1 실습 코드 정리(W6)  (0) 2024.04.14
컴프 1 실습 코드 정리 (W3~W5)  (0) 2024.04.08