Tuesday, May 8, 2007

Program Creat Menu

public class Menu {

public static void main(String[] args) {
char choice;
int i=1;
while(i==1){
System.out.println("1.Add");
System.out.println("2.Sub");
System.out.println("3.Mul");
System.out.println("4.Def");
System.out.println("5.Pow");
System.out.println("6.square");

choice=Console.readChar();
switch(choice){
case '1':
int a,b,c;
System.out.print("Enter A : ");
a=Console.readInt();
System.out.print("Enter B : ");
b=Console.readInt();
c=a+b;
System.out.println("Ans is "+c);
break;
case '2':
int a2,b2,c2;
System.out.print("Enter A : ");
a2=Console.readInt();
System.out.print("Enter B : ");
b2=Console.readInt();
c2=a2-b2;
System.out.println("Ans is "+c2);
break;
case '3':
int a3,b3,c3;
System.out.print("Enter A : ");
a3=Console.readInt();
System.out.print("Enter B : ");
b3=Console.readInt();
c3=a3*b3;
System.out.println("Ans is "+c3);
break;
case '4':
int a4,b4,c4;
System.out.print("Enter A : ");
a4=Console.readInt();
System.out.print("Enter B : ");
b4=Console.readInt();
c4=a4/b4;
System.out.println("Ans is "+c4);
break;
case '5':
int a5,b5;
double c5;
System.out.print("Enter A : ");
a5=Console.readInt();
System.out.print("Enter B : ");
b5=Console.readInt();
c5=Math.pow(a5,b5);
System.out.println("Power is "+c5);
break;
case '6':
int a6;
double b6;
System.out.print("Enter Square root : ");
a6=Console.readInt();
b6=Math.sqrt(a6);
System.out.println("Square root is "+(int)b6);
break;
case 'x':
i=0;
break;

}
}

}
}

Program Shape

public class ShapeMain {

public static void main(String[] args) {
Rectangle r1,r2;
Circle c1,c2;

r1 = new Rectangle();
r2 = new Rectangle(10,20);
c1 = new Circle();
c2 = new Circle(30);
r1.print();
r2.print();
c1.print();
c2.print();


}

}

Program OrderTest

public class OrderTest2 {
public static void main(String[] args) {
Order2 order1 = new Order2("Fried Rice", 2, 17);
Order2 order2 = new Order2("Fanta", 4, 6);

// order1 คือ Instance
// order2 คือ Instance

System.out.println("Order #1");
System.out.println(order1); // จะรู้อัตโนมัติว่าต้องไปเรียกใช้ ToString
System.out.println("Order #2");
System.out.println(order2); // จะรู้อัตโนมัติว่าต้องไปเรียกใช้ ToString
System.out.println("-----------------------------");
Order2.setVAT(8.0f);
System.out.println("Order #1\n" + order1);
System.out.println("Order #2\n" + order2);
}
}

class Order2 {
private static double vat = 7.0f;

private String name;
private int amount;
private double unitPrice;

public Order2(String n, int num, double price) {
if(n == null || num < 0 || price < 0)
throw new RuntimeException("program error");

name = n;
amount = num;
unitPrice = price;
}

public String toString() {
String str;
double total;

total = amount * unitPrice * (1+vat/100);

str = "Item name : " + name + "\n" +
"Amount : " + amount + " unit(s)\n" +
"Unit price : " + unitPrice + " baht\n" +
"VAT : " + vat + "%\n" +
"Total price : " + total + " baht\n";
return(str);
}

public static void setVAT(double v) {
if(v > 0)
vat = v;
}
}

Program JobTest

public class JobTest {
public static void main(String[] args) {
Manager kanok;
Secretary supa;
Employee suwat;

suwat = new Employee("Suwat", 8000);
supa = new Secretary("Supa", 10000);
kanok = new Manager("Kanok", 25000, supa);

System.out.println(suwat);
System.out.println(supa);
System.out.println(kanok);

suwat.raiseSalary();
supa.raiseSalary();
kanok.raiseSalary();

System.out.println(suwat);
System.out.println(supa);
System.out.println(kanok);
}
}

class Employee {
private String name;
protected double salary;

public Employee(String name, double salary) {
this.name = name;
this.salary = salary;
}

public void raiseSalary() {
salary += salary * 0.1;
}


public String toString() {
String str;

str = "name : " + name + "\n" +
"salary: " + salary + "\n";
return(str);
}
}

class Secretary extends Employee {
public Secretary(String name, double salary) {
super(name, salary);
}

public void raiseSalary() {
salary += salary * 0.15;
}
}

class Manager extends Employee {
private Secretary secretary;

public Manager(String name,double salary,Secretary secretary) {
super(name, salary);
this.secretary = secretary;
}

public void raiseSalary() {
salary += salary * 0.2;
}

public String toString() {
String str;

str = super.toString() +
"secretary :\n" + secretary;
return(str);
}
}

Program DataPerson 2

public class DataPerson2 {
public static void main(String[] args){
Person2 P1,P2;
Student2 std1,std2,std3;
Teacher2 th1,th2;
Student2[] STD;


P1=new Person2("แดง","042814148",21);
P2=new Person2("ดำ","017007001",18);
std1=new Student2("สาคร","0875090940",21,"4914210019");
std2=new Student2("จารุวรรณ","0815463627",21,"4914210002");
std3=new Student2("นิตยา","0850000000",22,"4914210078");



// อาจารย์มีนักศึกษา ในความดูแล 2 คน
STD = new Student2[2];
STD[0]=std1;
STD[1]=std2;
th1=new Teacher2("สมชาย","0854903454",30,"วิทยาศาสตร์",STD);
// อาจารย์มีนักศึกษา ในความดูแล 3 คน
STD = new Student2[3];
STD[0]=std1;
STD[1]=std2;
STD[2]=std3;
th2=new Teacher2("วีัณะการุณ","0895467583",30,"วิศวกรรมศาสตร์",STD);
System.out.println(P1);
System.out.println(P2);
System.out.println(std1);
System.out.println(std2);
System.out.println(th1);
System.out.println(th2);

}

}
class Person2{
private String name;
private String telno;
private int age;
public Person2(String name,String telno,int age){
this.name=name;
this.telno=telno;
this.age=age;

}
public String toString(){
String str;
str="Name : "+name+"\n"+
"Telno: "+telno+"\n"+
"Age : "+age+"\n";
return(str);
}
public String getName(){
return name;
}
public String getTelno(){
return telno;
}
public int getAge(){
return age;
}
public void setName(String name){
this.name=name;
}
public void setTelno(String telno){
this.telno=telno;
}
public void setAge(int age){
this.age=age;
}
}
class Student2 extends Person2{
private String studentCode;

public Student2(String name,String telno,int age,String studentCode){
super(name,telno,age);
this.studentCode=studentCode;
}
public String toString(){
String str;
str=super.toString()+"StudentCode :"+studentCode+"\n";
return (str);
}
public String getStudentCode(){
return (studentCode);
}
public void setStudentCode(String studentCode){
this.studentCode=studentCode;
}

}
class Teacher2 extends Person2{
private String department;
private Student2[] studentList;
public Teacher2(String name,String telno,int age,String department){
super(name,telno,age);
this.department=department;
}
public Teacher2(String name,String telno,int age,String department,Student2[] STD){
super(name,telno,age);
this.department=department;
studentList = STD;
}
public String toString(){
String str;
int i;
str=super.toString()+"Department :"+department+"\n";
for(i = 0; i < studentList.length; i++)
str += "studentList "+ (i+1) +" : \n" +studentList[i];
return(str);
}
public String getDepartment(){
return (department);
}
public void setDepartment(String department){
this.department=department;
}
}

Program DataPerson 1

public class DataPerson {
public static void main(String[] args){
Person P1,P2;
Student std1,std2;
Teacher th1,th2;

P1=new Person("แดง","042814148",21);
P2=new Person("ดำ","017007001",18);
std1=new Student("สาคร","0875090940",21,"4914210019");
std2=new Student("จารุวรรณ","0815463627",21,"4914210002");
th1=new Teacher("สมชาย","0854903454",30,"วิทยาศาสตร์");
th2=new Teacher("วีัณะการุณ","0895467583",30,"วิศวกรรมศาสตร์");

System.out.println(P1);
System.out.println(P2);
System.out.println(std1);
System.out.println(std2);
System.out.println(th1);
System.out.println(th2);

}

}
class Person{
private String name;
private String telno;
private int age;
public Person(String name,String telno,int age){
this.name=name;
this.telno=telno;
this.age=age;

}
public String toString(){
String str;
str="Name : "+name+"\n"+
"Telno: "+telno+"\n"+
"Age : "+age+"\n";
return(str);
}
public String getName(){
return name;
}
public String getTelno(){
return telno;
}
public int getAge(){
return age;
}
public void setName(String name){
this.name=name;
}
public void setTelno(String telno){
this.telno=telno;
}
public void setAge(int age){
this.age=age;
}
}
class Student extends Person{
private String studentCode;

public Student(String name,String telno,int age,String studentCode){
super(name,telno,age);
this.studentCode=studentCode;
}
public String toString(){
String str;
str=super.toString()+"StudentCode :"+studentCode+"\n";
return (str);
}
public String getStudentCode(){
return (studentCode);
}
public void setStudentCode(String studentCode){
this.studentCode=studentCode;
}

}
class Teacher extends Person{
private String department;
public Teacher(String name,String telno,int age,String department){
super(name,telno,age);
this.department=department;
}
public String toString(){
String str;
str=super.toString()+"Department :"+department+"\n";
return(str);
}
public String getDepartment(){
return (department);
}
public void setDepartment(String department){
this.department=department;
}
}

Program DetailStudent

public class DetailStudent {

public static void main(String[] args) {
Detail std1 = new Detail("Fried","Rice",19,170);
Detail std2 = new Detail("Fanta","Four",15,172);
Detail std3 = new Detail("A2","Four",18,160);

// std1 คือ Instance
// std2 คือ Instance

System.out.println("นักเรียนคนที่ # "+std1.getCounter());
System.out.println(std1); // จะรู้อัตโนมัติว่าต้องไปเรียกใช้ ToString
System.out.println("นักเรียนคนที่ # "+std2.getCounter());
System.out.println(std2); // จะรู้อัตโนมัติว่าต้องไปเรียกใช้ ToString
System.out.println("นักเรียนคนที่ # "+std3.getCounter());
System.out.println(std3); // จะรู้อัตโนมัติว่าต้องไปเรียกใช้ ToString

}
}
class Detail {
private static double total=0;
private String firstname;
private String lastname;
private int age;
private double height;
private double counter;

public Detail(String firstname, String lastname, int age,double height) {

this.firstname = firstname;
this.lastname = lastname;
this.age = age;
this.height =height;
total++;
counter=total;

}

public String toString() {
String str;
str = "Firstname : " + firstname + " \n" +
"Lastname : " + lastname + " \n" +
"Age : " + age + " Years.\n"+
"Height : " + height + " cm.\n";


return(str);
}
public double getCounter(){
return counter ;
}
public void setCounter(double cout){
counter=cout ;
}
}