Advertisement
MrDoyle

OOP) Intro version 2

Apr 9th, 2021
429
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.25 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Student {
  4.  
  5.     //instance variables, details we want to associate with the Student class
  6.     int studentID;
  7.     String name;
  8.     int age;
  9.     String favouriteSubject;
  10.     String sex;
  11.     double GPA;
  12.  
  13.     //Constructor for when you initialise the class, the access point
  14.     public Student (String studentName, int studentAge, double studentGPA ) {
  15.         name = studentName;
  16.         age = studentAge;
  17.         GPA = studentGPA;
  18.     }
  19.  
  20.     //void means nothing leaves with the method, everything happens within
  21.     public void getName (){
  22.  
  23.         System.out.println("This students name is: " + name);
  24.     }
  25.  
  26.     public void getAge (){
  27.  
  28.         System.out.println(name +"´s age is " +age);
  29.     }
  30.  
  31.     //int means you will return an int when you leave the method
  32.     public int futureAge (){
  33.         int newAge = age * 10;
  34.         return newAge;
  35.     }
  36.  
  37.     //calling a method with a parameter
  38.     public void setStudentID (int sNumber){
  39.         studentID = sNumber;
  40.         System.out.println(name + "´s student ID number is: " + studentID);
  41.     }
  42.  
  43.     //accessing information from the class level
  44.     public void getStudentInfo (){
  45.         System.out.println("The student´s name is: " + name);
  46.         System.out.println("The student´s ID number is: " + studentID);
  47.         System.out.println("The student´s age is: " + age);
  48.         System.out.println("The student´s GPA is: " + GPA);
  49.     }
  50.  
  51.  
  52.     public static void main(String[] args) {
  53.  
  54.         double grade = 2.5;
  55.  
  56.         //instantiating an object gives you access to the class, via the constructor
  57.         Student testSubject1 = new Student("Bob", 12, grade);
  58.         Student testSubject2 = new Student("Jane", 10, 6.2);
  59.         Student testSubject3 = new Student("Trish", 11, 3.2);
  60.  
  61.  
  62.  
  63.         testSubject1.getName();
  64.         testSubject1.getAge();
  65.         System.out.println("your age multiplied by 10 = " + testSubject1.futureAge());
  66.  
  67.         //getting info from the user via a scanner
  68.         Scanner info = new Scanner(System.in);
  69.         System.out.print("Please input your student ID number:");
  70.         int sNumber =  info.nextInt();
  71.         testSubject1.setStudentID(sNumber);
  72.  
  73.         testSubject1.getStudentInfo();
  74.     }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement