Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Student {
- //instance variables, details we want to associate with the Student class
- int studentID;
- String name;
- int age;
- String favouriteSubject;
- String sex;
- double GPA;
- //Constructor for when you initialise the class, the access point
- public Student (String studentName, int studentAge, double studentGPA ) {
- name = studentName;
- age = studentAge;
- GPA = studentGPA;
- }
- //void means nothing leaves with the method, everything happens within
- public void getName (){
- System.out.println("This students name is: " + name);
- }
- public void getAge (){
- System.out.println(name +"´s age is " +age);
- }
- //int means you will return an int when you leave the method
- public int futureAge (){
- int newAge = age * 10;
- return newAge;
- }
- //calling a method with a parameter
- public void setStudentID (int sNumber){
- studentID = sNumber;
- System.out.println(name + "´s student ID number is: " + studentID);
- }
- //accessing information from the class level
- public void getStudentInfo (){
- System.out.println("The student´s name is: " + name);
- System.out.println("The student´s ID number is: " + studentID);
- System.out.println("The student´s age is: " + age);
- System.out.println("The student´s GPA is: " + GPA);
- }
- public static void main(String[] args) {
- double grade = 2.5;
- //instantiating an object gives you access to the class, via the constructor
- Student testSubject1 = new Student("Bob", 12, grade);
- Student testSubject2 = new Student("Jane", 10, 6.2);
- Student testSubject3 = new Student("Trish", 11, 3.2);
- testSubject1.getName();
- testSubject1.getAge();
- System.out.println("your age multiplied by 10 = " + testSubject1.futureAge());
- //getting info from the user via a scanner
- Scanner info = new Scanner(System.in);
- System.out.print("Please input your student ID number:");
- int sNumber = info.nextInt();
- testSubject1.setStudentID(sNumber);
- testSubject1.getStudentInfo();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement