Sunday, August 17, 2014

NETBEANS Java Basics 109 – Java Class Methods

---
Java Basics 109– Java Class and Methods

STEPS

1) Open Startup Project.

1-1  ) Continue from the previous tutorial, or create a new Java Application project using Project/Package Name exams and Main Class ExamDetails.



1-2  ) Your Main Class should contain the following codes.
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;
    }
}

2) Add method examScore

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");
        String score = aStudent.examScore(30);
        System.out.println(sName);
        System.out.println(exam);
        System.out.println(score);
    }
}
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;
    }
    String examScore(int aScore) {
        Exam_Score = aScore + " out of 50";
        return Exam_Score;
    }
}

3) Add method  examGrade and getGrade.

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");
        String score = aStudent.examScore(30);
        String grade = aStudent.examGrade(30);
        System.out.println(sName);
        System.out.println(exam);
        System.out.println(score);
        System.out.println(grade);
    }
}
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;
    }
    String examScore(int aScore) {
        Exam_Score = aScore + " out of 50";
        return Exam_Score;
    }
    private String getGrade(int aScore) {
        String examGrade = "";
        if (aScore >= 0 && aScore <= 10) {
            examGrade = "E";
        } else if (aScore >= 11 && aScore <= 20) {
            examGrade = "D";
        } else if (aScore >= 21 && aScore <= 30) {
            examGrade = "C";
        } else if (aScore >= 31 && aScore <= 40) {
            examGrade = "B";
        } else if (aScore >= 41) {
            examGrade = "A";
        }
        return "Grade is " + examGrade;
    }
    String examGrade(int aScore) {
        Exam_Grade = this.getGrade(aScore);
        return Exam_Grade;
    }
}

REFERENCE

---

No comments:

Post a Comment