Mga 'tol,
Another code refactoring para mas madali at maayos mag program.
Using Java CLASS (or template, or blueprint). A preview to Object Oriented Programming (OOP).
(To learn more, join SERIOUSLY JAVA group)
****NOTE, save all these 3 files in the same project folder******
***LONG METHOD, hindi naka refactor*********
Save it as "StudentApp1.java"
***GAGAMIT tayo ng Class or template or blueprint*********
Save it as "StudentTemplate.java"
***GAGAMITIN NATIN yung CLASS to "refactor"*********
Save it as "StudentApp2.java"
TAKE NOTE: ****Puwede pa ito i-refactor further by adding toString() method and "setter" methods. Assignment nyo na yan.
Another code refactoring para mas madali at maayos mag program.
Using Java CLASS (or template, or blueprint). A preview to Object Oriented Programming (OOP).
(To learn more, join SERIOUSLY JAVA group)
****NOTE, save all these 3 files in the same project folder******
***LONG METHOD, hindi naka refactor*********
Save it as "StudentApp1.java"
Code:
/**
* Refactoring series
* @author CodyScott for PHCorner
*
*/
public class StudentApp1 {
public static void main(String[] args) {
String[] studentInformation = {"Daniel","De La Cruz","24", "BS Civil Engineering", "University of the Philippines"};
String firstName = studentInformation[0];
String lastName = studentInformation[1];
String age = studentInformation[2];
String course = studentInformation[3];
String school = studentInformation[4];
System.out.println("STUDENT 1 INFORMATION");
System.out.println("First Name: " + firstName);
System.out.println("Last Name: " + lastName);
System.out.println("Age: " + age);
System.out.println("Course: " + course);
System.out.println("School: " + school);
System.out.println("*************************");
String[] studentInformation2 = {"Mark","Santos","18", "BS Nursing", "Adamson U"};
String firstName2 = studentInformation2[0];
String lastName2 = studentInformation2[1];
String age2 = studentInformation2[2];
String course2 = studentInformation2[3];
String school2 = studentInformation2[4];
System.out.println("STUDENT 2 INFORMATION");
System.out.println("First Name: " + firstName2);
System.out.println("Last Name: " + lastName2);
System.out.println("Age: " + age2);
System.out.println("Course: " + course2);
System.out.println("School: " + school2);
}
}
***GAGAMIT tayo ng Class or template or blueprint*********
Save it as "StudentTemplate.java"
Code:
public class StudentTemplate{
private String firstName;
private String lastName;
private String age;
private String course;
private String school;
public StudentTemplate(String fName,String lName,String edad,String cors,String skol) {
this.firstName = fName;
this.lastName = lName;
this.age = edad;
this.course = cors;
this.school = skol;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public String getAge() {
return age;
}
public String getCourse() {
return course;
}
public String getSchool() {
return school;
}
}
***GAGAMITIN NATIN yung CLASS to "refactor"*********
Save it as "StudentApp2.java"
Code:
/**
* Refactoring series
* A preview to using Class (Object Oriented Programming)
* @author CodyScott for PHCorner
*
*/
public class StudentApp2 {
public static void main(String[] args) {
StudentTemplate student1 = new StudentTemplate("Daniel","De La Cruz","24", "BS Civil Engineering", "University of the Philippines");
System.out.println("STUDENT INFORMATION");
System.out.println("First Name: " + student1.getFirstName());
System.out.println("Last Name: " + student1.getLastName());
System.out.println("Age: " + student1.getAge());
System.out.println("Course: " + student1.getCourse());
System.out.println("School: " + student1.getSchool());
System.out.println("*************************");
StudentTemplate student2 = new StudentTemplate("Mark","Santos","18", "BS Nursing", "Adamson U");
System.out.println("STUDENT INFORMATION");
System.out.println("First Name: " + student2.getFirstName());
System.out.println("Last Name: " + student2.getLastName());
System.out.println("Age: " + student2.getAge());
System.out.println("Course: " + student2.getCourse());
System.out.println("School: " + student2.getSchool());
}
}
TAKE NOTE: ****Puwede pa ito i-refactor further by adding toString() method and "setter" methods. Assignment nyo na yan.