λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°
μ‹œλ„/ꡭ비지원

[D+13] 코딩데이

by πŸ‡λ°•λ΄‰λ΄‰πŸ‡ 2022. 10. 10.

 

 

ꡭ비지원 D+13

-  코딩데이 -

 


 

Q) ν•™μƒμ˜ 성적및 κ°œμΈμ •λ³΄λ₯Ό κ΄€λ¦¬ν•˜λŠ” 클래슀λ₯Ό λ§Œλ“œμ‹œμ˜€.

 

• main

import java.util.Scanner;

public class Test1 {

	public static void main(String[] args) {
		// 학생(Student1) 클래슀λ₯Ό λ§Œλ“œμ„Έμš”
		// 이름, ν•™λ²ˆ 
		// ꡭ어성적, μ˜μ–΄μ„±μ , μˆ˜ν•™μ„±μ  
		// 총점을 κ΅¬ν•˜λŠ” λ©”μ†Œλ“œ 
		// 평균을 κ΅¬ν•˜λŠ” λ©”μ†Œλ“œ 
		// 이름 및 ν•™λ²ˆμ„ λ¦¬ν„΄ν•˜λŠ” λ©”μ†Œλ“œ 
		// 기타 μΆ”κ°€μ μœΌλ‘œ ν•„μš”ν•œ λ©”μ†Œλ“œκ°€ μžˆλ‹€λ©΄ μΆ”κ°€ κ΅¬ν˜„ 
		
		Scanner scan = new Scanner(System.in);
		
		Student1 st = new Student1();
		
		System.out.print("이름 : ");
		String name = scan.next();
		
		System.out.print("ν•™λ²ˆ : ");
		int id = scan.nextInt();
		
		System.out.print("ꡭ어성적 : ");
		int kor = scan.nextInt();
		
		System.out.print("μ˜μ–΄μ„±μ  : ");
		int eng = scan.nextInt();
		
		System.out.print("μˆ˜ν•™μ„±μ  : ");
		int math = scan.nextInt();
		
		
		st.setName(name);
		st.setId(id);
		st.setKor(kor);
		st.setEng(eng);
		st.setMath(math);
		
		st.calSum();
		st.setSum();
		
		st.calAvg();
		st.setAvg();
		
		
		System.out.println("=============================");
		
		System.out.println(st.getId() + "번 " +st.getName() + "λ‹˜μ˜ 총점과 ν‰κ· μž…λ‹ˆλ‹€. ");
		System.out.println("총점 : " + st.getSum());
		System.out.printf("평균 : %.1f", st.getAvg());
		System.out.println("");
		
		System.out.println("=============================");
		
	}

}

 

• Student1 Class

public class Student1 {
	
	// λ³€μˆ˜ μ„ μ–Έ 
	private String name;		// 이름 
	private int id; 			// ν•™λ²ˆ 
	
	private int kor;			// ꡭ어성적 
	private int eng;			// μ˜μ–΄μ„±μ   
	private int math;			// μˆ˜ν•™μ„±μ  
	
	private double sum;			// 총합 
	private double avg;			// 평균 
	
	///////////////////////////////////////////////////
	// Setter 
	
	public void setName(String name)
	{
		this.name = name;
	}
	
	public void setId(int id)
	{
		this.id = id;
	}
	
	public void setKor(int kor)
	{
		this.kor = kor;
	}
	
	public void setEng(int eng)
	{
		this.eng = eng;
	}
	
	public void setMath(int math)
	{
		this.math = math;
	}
	
	public void setSum()
	{
		sum = calSum();
	}
	
	public void setAvg()
	{
		avg = calAvg();
	}

	///////////////////////////////////////////////////
	// Getter
	
	public String getName()
	{
		return name;
	}
	
	public int getId()
	{
		return id;
	}
	
	public int getKor()
	{
		return kor;
	}
	
	public int getEng()
	{
		return eng;
	}
	
	public int getMath()
	{
		return math;
	}
	
	public double getSum()
	{
		return sum;
	}
	
	public double getAvg()
	{
		return avg;
	}
	
	///////////////////////////////////////////////////
	// Calculate sum, avg
	
	public int calSum()
	{
		return getKor() + getEng() + getMath();
	}
	
	public double calAvg()
	{
		return getSum() / 3;
	}
}

 

 

 

Q) 1μ°¨ν•¨μˆ˜λ₯Ό λ§Œλ“œλŠ” 클래슀λ₯Ό μž‘μ„±ν•˜μ‹œμ˜€.

 

• main

import java.util.Scanner;

public class Test2 {

	public static void main(String[] args) {
		// 1μ°¨ ν•¨μˆ˜λ₯Ό λ§Œλ“œλŠ” 방식은 3가지가 μžˆμŠ΅λ‹ˆλ‹€. 
		// 1> ν•œμ μ˜ μ’Œν‘œμ™€ 기울기λ₯Ό μ•Œ 경우 
		// 2> λ‘μ μ˜ μ’Œν‘œλ₯Ό μ•Œ 경우 
		
		// μž…λ ₯ : 
		// 1> ν•œμ μ˜ μ’Œν‘œμ™€ 기울기λ₯Ό μž…λ ₯ 
		// 2> λ‘μ μ˜ μ’Œν‘œλ₯Ό μž…λ ₯ 
		
		// 좜λ ₯ :
		// y = ax + b ν˜•νƒœλ‘œ 좜λ ₯  
		
		Scanner scan = new Scanner(System.in);
		Function2 fn = new Function2();
		
		System.out.println("ν•œμ μ˜ μ’Œν‘œκ³Ό 기울기λ₯Ό μ•Œκ²½μš° 1λ²ˆμ„,");
		System.out.println("λ‘μ μ˜ μ’Œν‘œλ₯Ό μ•Œκ²½μš° 2λ²ˆμ„ λˆŒλŸ¬μ£Όμ„Έμš”.");
		int choice  = scan.nextInt();
		fn.setChoice(choice);
		
		double a; // 기울기
		double x1, y1, x2, y2; // 점의 μ’Œν‘œ 	
		
		if(choice == 1)
		{
			System.out.print("xμ’Œν‘œλ₯Ό μž…λ ₯ν•΄ μ£Όμ„Έμš” :");
			x1 = scan.nextDouble();
			System.out.print("yμ’Œν‘œλ₯Ό μž…λ ₯ν•΄ μ£Όμ„Έμš” : ");
			y1 = scan.nextDouble();
			System.out.print("기울기λ₯Ό μž…λ ₯ν•΄ μ£Όμ„Έμš” : ");
			a = scan.nextDouble();
			
			fn.setA(a);
			fn.setX1(x1);
			fn.setY1(y1);
			fn.calB();
		}
		else if(choice == 2)
		{
			System.out.print("x1μ’Œν‘œλ₯Ό μž…λ ₯ν•΄ μ£Όμ„Έμš” :");
			x1 = scan.nextDouble();
			System.out.print("y1μ’Œν‘œλ₯Ό μž…λ ₯ν•΄ μ£Όμ„Έμš” : ");
			y1 = scan.nextDouble();
			System.out.print("x2μ’Œν‘œλ₯Ό μž…λ ₯ν•΄ μ£Όμ„Έμš” :");
			x2 = scan.nextDouble();
			System.out.print("y2μ’Œν‘œλ₯Ό μž…λ ₯ν•΄ μ£Όμ„Έμš” : ");
			y2 = scan.nextDouble();
			
			fn.setX1(x1);
			fn.setY1(y1);
			fn.setX2(x2);
			fn.setY2(y2);
			fn.calA();
			fn.calB();
		}
		
		fn.printFunction();

	}

}

 

 

• Function2 Class

public class Function2 {

	// λ³€μˆ˜ μ„ μ–Έ 
	private int choice;						// 초이슀 
	private double x1, y1, x2, y2;			// μ μ’Œν‘œ 
	private double a;						// 기울기 
	private double b;						// y절편 
	
	///////////////////////////////////////////////////
	// Setter
	
	public void setChoice(int choice)
	{
		this.choice = choice;
	}
	
	public void setX1(double x1)
	{
		this.x1 = x1;
	}
	
	public void setY1(double y1)
	{
		this.y1 = y1;
	}
	
	public void setX2(double x2)
	{
		this.x2 = x2;
	}
	
	public void setY2(double y2)
	{
		this.y2 = y2;
	}
	
	public void setA(double a)
	{
		this.a = a;
	}
	
	public void setB(double b)
	{
		this.b = b;
	}
	
	///////////////////////////////////////////////////
	// Getter

	public int getChoice()
	{
		return choice;
	}
	
	public double getX1()
	{
		return x1;
	}
	
	public double getY1()
	{
		return y1;
	}
	
	public double getX2()
	{
		return x2;
	}
	
	public double getY2()
	{
		return y2;
	}
	
	public double getA()
	{
		return a;
	}
	
	public double getB()
	{
		return b;
	}
	
	///////////////////////////////////////////////////
	// Calculate a, b
	
	public void calA()
	{
		if(getChoice() == 1)
		{
			return;
		}
		else if(getChoice() == 2)
		{
			a = (getY2() - getY1()) / (getX2() - getX1());
		}
	}
	
	public void calB()
	{
		b = getY1() - (getA() * getX1());
	}
	
	///////////////////////////////////////////////////
	// Print function
	
	public void printFunction()
	{
		if(a == 0)
		{
			if(b == 0)
			{
				System.out.println("y = 0");
			}
			else {
				System.out.println("y = " + b);
			}
		}
		else {
			if(b == 0)
			{
				System.out.println("y = " + a + "x");
			}
			else if(b < 0)
			{
				System.out.println("y = " + a + "x " + b);
			}
			else
			{
				System.out.println("y = " + a + "x + " + b);
			}
			
		}
	}
	
}

 

 

λ°˜μ‘ν˜•

λŒ“κΈ€