Saturday, August 16, 2014

NETBEANS Java Basics 108 – Java Class and Object

---
Java Basics 108 – Class and Object

STEPS

1) Create New Project.

Project Name: exams
Main Class: exams.ExamDetails
New Project created.
Make a copy of Startup Codes
package exams;
public class ExamDetails {
    public static void main(String[] args) {
        // TODO code application logic here
    }  
}

2) CREATE JAVA CLASS

2-1) Delete all original codes and paste the Startup Codes above.
package exams;
public class ExamDetails
{
        public static void main (String[] args){
        // TODO code application logic here
        }
}
2-2) Add second class StudentResults
package exams;
public class ExamDetails {
    public static void main(String[] args) {
        // TODO code application logic here
    }
}
class StudentResults {
}
2-3) Add properties to the StudentResults class.
package exams;
public class ExamDetails {
    public static void main(String[] args) {
        // TODO code application logic here
    }
}
class StudentResults {
    String Full_Name;
    String Exam_Name;
    String Exam_Score;
    String Exam_Grade;
}
2-4) Declare a StudentResults object called aStudent
package exams;
public class ExamDetails {
    public static void main(String[] args) {
        StudentResults aStudent = new StudentResults();
    }
}
class StudentResults {
    String Full_Name;
    String Exam_Name;
    String Exam_Score;
    String Exam_Grade;
}
2-5) Assign property value to aStudent
Try printing the value.
package exams;
public class ExamDetails {
    public static void main(String[] args) {
        StudentResults aStudent = new StudentResults();
        aStudent.Exam_Name = "VB.NET";
        String exam = aStudent.Exam_Name;
        System.out.println(exam);
    }
}
class StudentResults {
    String Full_Name;
    String Exam_Name;
    String Exam_Score;
    String Exam_Grade;
}
2-6) Change access to class property in StudentResults.
Try running the code.
package exams;
public class ExamDetails {
    public static void main(String[] args) {
                StudentResults aStudent= new StudentResults();
                aStudent.Exam_Name="VB.NET";
                String exam=aStudent.Exam_Name;
                System.out.println(exam);
        }
}
class StudentResults {
        private String Full_Name;
        private String Exam_Name;
        private String Exam_Score;
        private String Exam_Grade;
}
At this point, the compiler will output error message:
Comment (//) those lines that have got errors.
2-7) Add an initiating method (Constructor ) to StudentResults.
a) The name of the method is similar to the name of the class.
b) The parameters of the method are assigned to the property of the class.
package exams;
public class ExamDetails {
    public static void main(String[] args) {
        StudentResults aStudent = new StudentResults();
        //aStudent.Exam_Name = "VB.NET";
        //String exam = aStudent.Exam_Name;
        //System.out.println(exam);
    }
}
class StudentResults {
    private String Full_Name;
    private String Exam_Name;
    private String Exam_Score;
    private String Exam_Grade;
    StudentResults(String name, String grade) {
        Full_Name = name;
        Exam_Grade = grade;
    }
}
At this point, if you try to compile, you may get errors because the new object declaration parameter doesn’t  match with the constructor parameter declaration.
2-8) Change the constructor declaration.
a) Omitting parameter
b) Assigning default values to all class properties.
package exams;
public class ExamDetails {
    public static void main(String[] args) {
        StudentResults aStudent = new StudentResults();
    }
}
class StudentResults {
    private String Full_Name;
    private String Exam_Name;
    private String Exam_Score;
    private String Exam_Grade;
    StudentResults() {
                Full_Name="No name given";
                Exam_Name="Unknown";
                Exam_Score="No Score";
                Exam_Grade="Unknown";
    }
}
Note: delete the statement marked with “//”
2-9) Create method to change class property.
package exams;
public class ExamDetails {
    public static void main(String[] args) {
        StudentResults aStudent = new StudentResults();
    }
}
class StudentResults {
    private String Full_Name;
    private String Exam_Name;
    private String Exam_Score;
    private String Exam_Grade;
    StudentResults() {
        Full_Name = "No name given";
        Exam_Name = "Unknown";
        Exam_Score = "No Score";
        Exam_Grade = "Unknown";
    }
    String fullName(String aName) {
        Full_Name = aName;
        return Full_Name;
    }
}
2-10) Edit the main method.
a) Add a call to class method.
b) Add an output statement to print the class property value.
package exams;
public class ExamDetails {
    public static void main(String[] args) {
        StudentResults aStudent = new StudentResults();
        String sName = aStudent.fullName("Bill Gates");
        System.out.println(sName);
    }
}
class StudentResults {
    private String Full_Name;
    private String Exam_Name;
    private String Exam_Score;
    private String Exam_Grade;
    StudentResults() {
        Full_Name = "No name given";
        Exam_Name = "Unknown";
        Exam_Score = "No Score";
        Exam_Grade = "Unknown";
    }
    String fullName(String aName) {
        Full_Name = aName;
        return Full_Name;
    }
}
2-11) Add another method to class StudentResults
package exams;
public class ExamDetails {
    public static void main(String[] args) {
        StudentResults aStudent = new StudentResults();
        String sName = aStudent.fullName("Bill Gates");
        System.out.println(sName);
    }
}
class StudentResults {
    private String Full_Name;
    private String Exam_Name;
    private String Exam_Score;
    private String Exam_Grade;
    StudentResults() {
        Full_Name = "No name given";
        Exam_Name = "Unknown";
        Exam_Score = "No Score";
        Exam_Grade = "Unknown";
    }
    String fullName(String aName) {
        Full_Name = aName;
        return Full_Name;
    }
        String examName(String examCode){
                if (examCode.equals("VB")){
                        Exam_Name="Visual Basic.NET";
                }
                else if (examCode.equals("JV")){
                        Exam_Name="Java";
                }
                else if (examCode.equals("C#")){
                        Exam_Name="C# .NET";
                }
                else if (examCode.equals("PH")){
                        Exam_Name="PHP";
                }
                else {
                        Exam_Name="No Exam Selected";
                }
                return Exam_Name;
        }
}
2-12) Edit main method
a) Add a call to the new class method.
b) Add an output statement to print the returning value.
package exams;
public class ExamDetails {
    public static void main(String[] args) {
        StudentResults aStudent = new StudentResults();
        String sName = aStudent.fullName("Bill Gates");
        String exam = aStudent.examName("VB");
        System.out.println(sName);
        System.out.println(exam);
    }
}
class StudentResults {
    private String Full_Name;
    private String Exam_Name;
    private String Exam_Score;
    private String Exam_Grade;
    StudentResults() {
        Full_Name = "No name given";
        Exam_Name = "Unknown";
        Exam_Score = "No Score";
        Exam_Grade = "Unknown";
    }
    String fullName(String aName) {
        Full_Name = aName;
        return Full_Name;
    }
    String examName(String examCode) {
        if (examCode.equals("VB")) {
            Exam_Name = "Visual Basic.NET";
        } else if (examCode.equals("JV")) {
            Exam_Name = "Java";
        } else if (examCode.equals("C#")) {
            Exam_Name = "C# .NET";
        } else if (examCode.equals("PH")) {
            Exam_Name = "PHP";
        } else {
            Exam_Name = "No Exam Selected";
        }
        return Exam_Name;
    }
}
3) More Class Methods

REFERENCE

---

No comments:

Post a Comment