Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

What this Lab Is About: Classes and objects Familiarization with default or no-argument constructor and constructor with arguments. Defining void methods and return value methods.

What this Lab Is About: Classes and objects Familiarization with default or no-argument constructor and constructor with arguments. Defining void methods and return value methods. Use the following Coding Guidelines: When declaring a variable, you usually want to initialize it. Remember you cannot initialize a number with a string. Remember variable names are case sensitive. Use tabs or spaces to indent code within blocks (code surrounded by braces). Use white space to make your program more readable. Use comments after the ending brace of classes, methods, and blocks to identify to which block it belongs. Problem Description: For this Lab you have to implement a class Student. Student class should have instance variables studentMajor, firstName, lastName, studentCredits, and studentPoints. For Student class, supply two constructors and the following methods: getMajor(), getGradepoints(), getCredits() , getFullName(), loopHelper(int start, int end, int incBy). Step 1: Getting Started Create a class called Lab8. Be sure to name your file Lab8.java At the beginning of each programming assignment you must have a comment block with the following information: /*------------------------------------------------------------------------- // AUTHOR: your name. // FILENAME: title of the source file. // SPECIFICATION: your own description of the program. // FOR: CSE 110- Lab #8 // TIME SPENT: how long it took you to complete the assignment. //-----------------------------------------------------------*/ Step 2: Create a Separate class Student.java. Declaring Class Examining the problem, we need to create a Student class, declare Student class as follows public class Student{ } Inside the class, declare some data members: a String variable called studentMajor, a String variable firstName, a String variable lastName, an int called studentCredits, and an int called studentPoints. // declare some variables of different types: // a string called studentMajor //--> // an int called studentCredits //--> // an int called studentPoints // a string called firstName // a string called lastName //--> Step 3: Defining the constructors: Remember the constructor with arguments assigns input data to the data members. In this case, we have an example of constructors overloading. If user provides only First name and last name, then Constructor 1 is being called and rest of variables are initialized to default values, if user provides all variables, then java calls Constructor 2. Constructor 1: public Student (String fName, String lName) { // write the segment of code //that assigns fName and Lname to variables. //-> // The rest of variables are initialized to default values this.studentMajor = "General Studies"; this.studentPoints = 0; this.studentCredits = 0; } Constructor 2: public Student (String major, int credits, int points, String fName, String lName) { // write the segment of code //that assigns input data to the data members } Step4: Supply the methods You need to supply the following get methods: getMajor() to get the major of the Student object. getGradepoints() to get the number of points of the Student object. getCredits() to get the total credits of the Student object. getFullName() to get the full name of the Student. loopHelper(int start, int end, int incBy) to print sequence of number with given starting point, end point and increment integer. Hint: These method contains a line of code that returns the desired variable value(major...). public String getMajor() { // write a line of code //that returns the student major } public int getGradePoints () { // write a line of code //that returns the student grade points } public int getCredits() { // write a line of code //that returns the student total credits } public String getFullName() { // write a line of code //that returns the full name. Return (firstName + " " + lastName); } Also, you should have a changeMajor method. The method will take only a string and will change the Student objects major variable to the new input. public void ChangeMajor( String newMajor) { // Change the value of the Student objects major // variable to the new inputs value. } you should have a loopHelper method. Given a start integer, end integer and incBy integer, it will print sequence starting from start till end, incremented by given incBy integer. See example below. public void loopHelper(int start, int end, int incBy){ int sum = 0; // write a line of code // Using given inputs use for loop that // Prints sequence of numbers and summation. // Example // -> Calling student1.loopHelper(1, 10, 3); where start is 1, end is 10 and increment by 3, // We print following results using System.out.print // 1 4 7 10 //Sum is 22 System.out.println(); System.out.println ("Sum is " + sum); } Step 5: Calling the constructor of Student class from the main method. To use Student class variables and methods we need to have a runner function. For any class in java, main method will be the running method. Your class Lab8 will have the main method. To use the Student class variables and methods, you need to use the constructor to create an object of Student Class. public class Lab8 { public static void main(String[] args) { //declare variables where you will store //inputs from user --> // declare a Scanner object --> //prompt the user for first student inputs //firstName and lastName. // store the input in the declared variables --> //use the constructor 1 with two arguments //to create a brand-new object Student //called Student1 using the variable //values provided by the user --> //prompt the user for second student inputs // studentMajor // studentCredits, studentGradePoints, firstName and lastName. --> // store the input in the declared variables --> //use the constructor with arguments //to create a brand-new object Student //called Student2 using the variable //values provided by the user --> } } Hint: Do not forget to import the Scanner package at the very top of your program: import java.util.Scanner; Step 5: Calling Student class methods and display the output The methods in the Student class will display the required outputs. //Call the get methods( getMajor...) //to display Student1 //major, grade points, and number of credits. // call the getFullName method to get the full name of the student. --> System.out.println(FIRST STUDENT INFORMATIONS ); System.out.println("The name of student is: "Major is: " < Student1.getMajor()> ". " "The students number of points is " + " " "Number of earned credits is " ". " // Change just the students major to // International Affairs by calling changeMajor(String newMajor) // Print out the following: // has changed majors to //to display Student2 //major, grade points, and number of credits. // call the getFullName method to get the full name of the student. --> System.out.println(SECOND STUDENT INFORMATIONS ); System.out.println("The name of student is: "Major is: " < Student2.getMajor()> ". " "The students number of points is " + " " "Number of earned credits is " ". " //to display Student2 //major, grade points, and number of credits. // call the getFullName method to get the full name of the student. --> System.out.println("STUDENT HELPER FUNCTIONS"); System.out.println("Iterate from 1 till 30 with 3 steps and find sum"); student1.loopHelper(1, 30, 3); System.out.println("Iterate from 1 till 30 with 3 steps and find sum student1.loopHelper(5, 28, 2); Step 7: Display the output See the sample output below and make sure to display is the same format. Sample Output: Below is an example of what your output should roughly look like when this lab is completed. All text in bold represents user input Sample Run 1: ENTER FIRST STUDENT INFORMATIONS Enter first name John Enter last name Terry FIRST STUDENT INFORMATIONS The name of the student is: John Terry Major is: General Studies The student's number of points is: 0 Number of earned credits is: 0 ENTER SECOND STUDENT INFORMATIONS Enter first name Ada Enter last name Lovelace Enter your major ElectricalEngineering Enter your grade Points 100 Enter your total credits 345 SECOND STUDENT INFORMATIONS The name of the student is: Ada Lovelace Major is: ElectricalEngineering The student's number of points is: 100 Number of earned a is: 345 Ada Lovelace has changed majors to International Affairs STUDENT HELPER FUNCTIONS Iterate from 1 till 30 with 3 steps and find sum 1 4 7 10 13 16 19 22 25 28 Sum is 145 Iterate from 5 till 28 with 2 steps and find sum 5 7 9 11 13 15 17 19 21 23 25 27 Sum is 192

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2014 Nancy France September 15 19 2014 Proceedings Part 2 Lnai 8725

Authors: Toon Calders ,Floriana Esposito ,Eyke Hullermeier ,Rosa Meo

2014th Edition

3662448505, 978-3662448502

More Books

Students also viewed these Databases questions