SHARE
TWEET

Untitled

a guest Sep 29th, 2018 272 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package student;
  7.  
  8. import java.util.Scanner;  //necessary because we are using Scanner to get input from the user.
  9.  
  10. /**
  11.  *
  12.  * @author tony
  13.  */
  14. public class Student {
  15.  
  16.     private String studentName;    //  Field/Instance Variables
  17.     private String modeOfStudy;
  18.     private int academicYear;
  19.     private double gradeAverage;
  20.  
  21.     public Student() {     //Constructor which takes no arguments. Why? To provide default values in case the user doesn't enter anything.
  22.         studentName = "Unknown";
  23.         modeOfStudy = "xxx";
  24.         academicYear = 1;
  25.         gradeAverage = 2.0;
  26.  
  27.     }
  28.    
  29.     public String toString()  //method that allows us to output the fields of the Student objects.
  30.         {
  31.             return studentName+" "+modeOfStudy+" "+academicYear+" "+gradeAverage;
  32.         }
  33.  
  34.     public Student(String name, String mode, int year, double grade) {  //Constructor which does take arguments, then assigning them to the field variables.
  35.         studentName = name;
  36.         modeOfStudy = mode;
  37.         academicYear = year;
  38.         gradeAverage = grade;
  39.     }
  40.  
  41.     public String studentName() {  //used to get user input from console. This could have been performed in the main method,
  42.         //I elected to do it here, for readability.  
  43.  
  44.         System.out.println("Please enter your name:");
  45.         Scanner user_input = new Scanner(System.in);
  46.         String studentName;
  47.         studentName = user_input.nextLine();
  48.         return studentName;
  49.  
  50.     }
  51.  
  52.     public void nameValidation(String a) {  //
  53.         if (a.isEmpty()) {
  54.             System.out.println("Empty string detected. Enter a value.");
  55.         } else {
  56.             System.out.println("Thank you for your entry.");
  57.         }
  58.        
  59.        
  60.  
  61.     }
  62.  
  63.     /**
  64.      * @param args the command line arguments
  65.      */
  66.     public static void main(String[] args) {
  67.         Student student = new Student();    //Default constructor for Java, and creating all objects of class (instantiating them) requires the new keyword.
  68.         Student student1 = new Student("Jason Voorhees", "full-time", 2, 3.0);
  69.         String a = student.studentName();   //Here, we are making a method call TO the studentName method stored in the Student class.
  70.         student.nameValidation(a); //Here, the nameValidation method in the Student class is making the call.
  71.        
  72.  
  73.        
  74.         //The code below is simply used to output and visually inspect that the constructor(s) have indeed worked correctly.
  75.        
  76.        
  77.         System.out.println( student );
  78. System.out.println( student1 );
  79.  
  80.     }
  81.  
  82. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top