java 0507

장진영·2024년 5월 7일
package test;

public class test1 {
	public static void main(String[] args) {
        String[] names = {"Sam", "Kate", "John", "Jenny" };
        
        System.out.println(names[0]);
        System.out.println(names[1]);
        System.out.println(names[2]);
        System.out.println(names[3]);
	}
}

=================================================

package test;

public class test1 {
	public static void main(String[] args) {
        int[] arr = { 88, 76, 92, 68, 55, 48, 82 };
        int sum = 0;
        
        for (int i =0; i < arr.length; i++) {
        	sum+= arr[i];
        }
        System.out.printf("총합: %d", sum);
	}
}

=================================================

package test;

public class test1 {
	public static void main(String[] args) {
        int[] evens = { 0, 2, 4, 6, 8, 10, 12, 14, 16, 18};
        int[] primes = { 2, 3, 5, 7, 11, 13, 17, 19};
        
        int evenSum = sum(evens);
        int primeSum = sum(primes);
        
        System.out.printf("짝수 배열의 합: %d\n", evenSum);
        System.out.printf("소수 배열의 합: %d\n", primeSum);
	}
	
	public static int sum(int[] arr) {
		int sum = 0;
		for (int i = 0; i < arr.length; i++) {
			sum+= arr[i];
		}
		return sum;
	}
}

=================================================

package test;

public class test1 {
	String name;
	String breeds;
	int age;
	
	void wag() {
		System.out.printf("[%s] 살랑살랑~\n", name);
	}
	void bark() {
		System.out.printf("[%s] 멍멍!\n", name);
	}
	void bark(int times) {
		String sound = "컹컹!";
		System.out.printf("[%s] %s(x%d)\n", name, sound, times);
	}
public static void main(String[] args) {
	test1 d1 = new test1();
	test1 d2 = new test1();
	
	d1.name = "망고";
	d1.breeds = "골든리트리버";
	d1.age = 2;
	d2.name = "까미";
	d2.breeds = "믹스";
	d2.age = 3;
	
	System.out.printf("d1 => {%s, %s, %d세}\n", 
			d1.name, d1.breeds, d1.age);
	System.out.printf("d2 -> {%s, %s, %d세}\n", d2.name, d2.breeds, d2.age);
	
	d1.wag();
	d2.bark();
	d1.bark(3);
}
}

===================================================

package HeroTest;

public class HeroTest {
	public static void main(String[] args) {
		Hero ironMan = new Hero("아이언맨", 100, 200);
		Hero hulk = new Hero("헐크", 200, 80);
		
		System.out.println(ironMan.toStr());
		System.out.println(hulk.toStr());
	}
}
class Hero {
	String name;
	int power;
	int speed;
	
	Hero(String n , int p , int s){
		name = n;
		power = p;
		speed = s;
	}
	
	String toStr() {
		return String.format("Hero{name: %s, power: %d, speed: %d}", name, power, speed);
	}
}

=====================================================

package Cylinder;

public class Cylinder {
	int radius;
	int height;
	
	double getVolume() {
		return radius * radius * Math.PI * height;
	}
	
	double getSurfaceArea() {
		double circleArea = Math.PI * radius * radius;
		double rectangleArea = height * 2 * Math.PI * radius;
		return 2 * circleArea + rectangleArea;
	}
	
	public static void main(String[] args) {
		Cylinder c = new Cylinder();
		c.radius = 4;
		c.height = 5;
		
		System.out.printf("원기둥의 부피: %.2f\n", c.getVolume());
		System.out.printf("원기둥의 겉넓이: %.2f\n", c.getSurfaceArea());
	}

}

========================================================

package Cylinder2;

public class Cylinder2 {
	int radius;
	int height;
	
	double getVolume() {
		return radius * radius * height;
	}
	double getSurface() {
		return radius * radius * 6;
	}
	
	public static void main(String[] args) {
		Cylinder2 c = new Cylinder2();
		c.radius = 5;
		c.height = 5;

	System.out.printf("정육면체의 부피: %.2f\n", c.getVolume());
	System.out.printf("정육면체의 겉넓이: %.2f\n", c.getSurface());
	}
}
profile
안녕하세요. 배운 것을 메모하는 velog입니다.

0개의 댓글