Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*-------------------------------------------------------------------------
- // AUTHOR: Orkhan Taghizada.
- // FILENAME: Lab7.
- // SPECIFICATION: Making program for students.
- // FOR: CSE 110- Lab #7
- // TIME SPENT: 20 Minutes.
- //-----------------------------------------------------------*/
- public class Student {
- // declare some variables of different types:
- // an int called studentID
- int studentId;
- // a string called studentMajor
- String studentMajor;
- // an int called studentCredits
- int studentCredits;
- // an int called studentPoints
- int studentPoints;
- // a string called firstName
- String firstName;
- // a string called lastName
- String lastName ;
- public Student (int id, String major, int credits, int points, String fName, String lName)
- {
- studentId = id;
- studentMajor=major;
- studentCredits=credits;
- studentPoints =points;
- firstName =fName;
- lastName = lName;
- // write the segment of code
- //that assigns input data to the data members
- }
- public int getID()
- {
- // write a line of code
- //that returns the student ID
- return getID();
- }
- public String getMajor()
- {
- // write a line of code
- //that returns the student major
- return getMajor();
- }
- public int getGradePoints ()
- {
- // write a line of code
- //that returns the student grade points
- return getGradePoints ();
- }
- public int getCredits()
- {
- // write a line of code
- //that returns the student total credits
- return getCredits();
- }
- public String getFullName()
- {
- // write a line of code
- //that returns the full name.
- // for this functionality, you need to create a private method ‘createFullName’
- // you have to call this private method here and then return the fullName. Like
- return createFullName();
- }
- private String createFullName()
- {
- // use the concatenation of firstName and lastName
- // to create full name and then return the full name.
- String FullName = firstName + lastName;
- return FullName;
- }
- public void changeMajor(String newMajor, int newPoints, int newCredits) {
- if (studentPoints<newPoints && studentCredits<newCredits){
- // Change the value of the Student object’s major
- // variable to the new input’s value.
- studentMajor = newMajor;
- studentCredits =newCredits;
- studentPoints=newPoints;
- System.out.println(getFullName() + "has changed majors to" + newMajor);
- }else {
- System.out.println("Invalid attempt to change major");
- }
- }}
- import java.util.Scanner;
- public class Lab7
- {
- public static void main(String[] args)
- {
- Scanner in = new Scanner(System.in);
- //declare variables where you will store
- //inputs from user
- int studentID;
- String studentMajor;
- int studentCredits;
- int studentGradePoints;
- String firstName;
- String lastName;
- System.out.println("Enter first name :" );
- firstName=in.next();
- System.out.println("Enter lst name :");
- lastName=in.next();
- System.out.println("Enter student ID " );
- studentID=in.nextInt();
- System.out.println("Enter student major :");
- studentMajor=in.next();
- System.out.println("Enter the number of credits earned by the student");
- studentCredits =in.nextInt();
- System.out.println("Enter student's number of Points");
- studentGradePoints=in.nextInt();
- //Call the get methods(getID, getMajor...)
- //to display Student1
- //ID, major, grade points, and number of credits.
- // call the getFullName method to get the full name of the student.
- Student Student1 = new Student( studentID, studentMajor, studentCredits, studentGradePoints, firstName , lastName);
- System.out.println("STUDENT INFORMATIONS \n");
- System.out.println("The name of student is: "+
- Student1.getFullName()+"\n");
- System.out.println("The student ID number is : "
- +Student1.getID()+ "\n");
- System.out.println("Major is: "
- + Student1.getMajor()+ ".\n");
- System.out.println("The student’s number of points is "
- + Student1.getGradePoints()+"\n");
- System.out.println("Number of earned credits is "
- +Student1.getCredits()+ ".\n");
- // Attempt to change the major to
- // “International Affairs” with 400 points and 500 (you can use other values as well)
- //by calling changeMajor(String newMajor, int newPoints, int newCredits)
- // credits. This should not succeed. It should print error.
- // Change just the student’s major to
- // “International Affairs” by calling changeMajor(String newMajor)
- // Print out the following:
- // <Student full name> has changed majors to <Student Major>
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement