Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package student;
- import java.util.Scanner; //necessary because we are using Scanner to get input from the user.
- /**
- *
- * @author tony
- */
- public class Student {
- private String studentName; // Field/Instance Variables
- private String modeOfStudy;
- private int academicYear;
- private double gradeAverage;
- public Student() { //Constructor which takes no arguments. Why? To provide default values in case the user doesn't enter anything.
- studentName = "Unknown";
- modeOfStudy = "xxx";
- academicYear = 1;
- gradeAverage = 2.0;
- }
- public String toString() //method that allows us to output the fields of the Student objects.
- {
- return studentName+" "+modeOfStudy+" "+academicYear+" "+gradeAverage;
- }
- public Student(String name, String mode, int year, double grade) { //Constructor which does take arguments, then assigning them to the field variables.
- studentName = name;
- modeOfStudy = mode;
- academicYear = year;
- gradeAverage = grade;
- }
- public String studentName() { //used to get user input from console. This could have been performed in the main method,
- //I elected to do it here, for readability.
- System.out.println("Please enter your name:");
- Scanner user_input = new Scanner(System.in);
- String studentName;
- studentName = user_input.nextLine();
- return studentName;
- }
- public void nameValidation(String a) { //
- if (a.isEmpty()) {
- System.out.println("Empty string detected. Enter a value.");
- } else {
- System.out.println("Thank you for your entry.");
- }
- }
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- Student student = new Student(); //Default constructor for Java, and creating all objects of class (instantiating them) requires the new keyword.
- Student student1 = new Student("Jason Voorhees", "full-time", 2, 3.0);
- String a = student.studentName(); //Here, we are making a method call TO the studentName method stored in the Student class.
- student.nameValidation(a); //Here, the nameValidation method in the Student class is making the call.
- //The code below is simply used to output and visually inspect that the constructor(s) have indeed worked correctly.
- System.out.println( student );
- System.out.println( student1 );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement