κ΅λΉμ§μ 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);
}
}
}
}
λ°μν
'μλ > κ΅λΉμ§μ' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[D+14] ν΄λμ€, μΆμν, μ°Έμ‘°νμ μμλ³μ (0) | 2022.10.11 |
---|---|
[D+13] Sort, Bubble Sort (0) | 2022.10.10 |
[D+12] class ν΄λμ€ (1) | 2022.10.05 |
[D+11] μ°Έμ‘°νμ (0) | 2022.10.04 |
[D+10] μ½λ©λ°μ΄ (0) | 2022.10.01 |
λκΈ