Question

In Java

a. Create a class called StudentRecord that has the following private variables: year, GPA Implement 2 constructors: default

0 0
Add a comment Improve this question Transcribed image text
Answer #1

1.

StudentRecord.java

public class StudentRecord implements Comparable<StudentRecord>{
private String firstName, lastName;
private int year;
private double gpa;
  
public StudentRecord()
{
this.firstName = "";
this.lastName = "";
this.year = 0;
this.gpa = 0.0;
}

public StudentRecord(String firstName, String lastName, int year, double gpa) {
this.firstName = firstName;
this.lastName = lastName;
this.year = year;
this.gpa = gpa;
}

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public int getYear() {
return year;
}

public void setYear(int year) {
this.year = year;
}

public double getGpa() {
return gpa;
}

public void setGpa(double gpa) {
this.gpa = gpa;
}
  
@Override
public String toString()
{
return ("First Name: " + getFirstName() + ", Last Name: " +
getLastName() + ", Year: " + getYear() + ", GPA: " + getGpa());
}

@Override
public int compareTo(StudentRecord record) {
int compare;
if(this.getGpa() < record.getGpa())
{
compare = -1;
}
else if(this.getGpa() > record.getGpa())
{
compare = 1;
}
else
compare = 0;
  
// if GPAs are equal
if(compare == 0)
{
compare = this.getYear() - record.getYear();
}
  
// if years are equal
if(compare == 0)
{
compare = this.getLastName().compareTo(record.getLastName());
}
  
// if last names are equal
if(compare == 0)
{
compare = this.getFirstName().compareTo(record.getFirstName());
}
return compare;
}
}

StudentRecordDriver.java (Client / Main class)

import java.util.ArrayList;
import java.util.Collections;

public class StudentRecordDriver {
  
public static void main(String[]args)
{
ArrayList<StudentRecord> studentRecords = new ArrayList<>();
studentRecords.add(new StudentRecord("Jes", "Reynaldsson", 1, 3.4));
studentRecords.add(new StudentRecord("Valeria", "Reynaldsson", 1, 3.7));
studentRecords.add(new StudentRecord("Valeria", "Reynaldsson", 1, 3.5));
studentRecords.add(new StudentRecord("Valeria", "Reynaldsson", 1, 3.68));
  
Collections.sort(studentRecords);
  
for(StudentRecord record : studentRecords)
{
System.out.println(record.toString());
}
}
}

un: First Name: Jes, Last Name: Reynaldsson, Year: 1, GPA: 3.4 First Name: Valeria, Last Nam Reynaldsson, Year 1, GPA: 3.5 Fi

**********************************************************************************************************************************************

2.

import java.util.Scanner;

public class SelectionSortIntegers {
  
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("How many integers you want to enter? ");
int n = sc.nextInt();
  
int arr[] = new int[n];
  
System.out.println("Enter the integers: ");
for(int i = 0; i < n; i++)
{
arr[i] = sc.nextInt();
}
  
performSelectionSort(arr);
}
  
public static void performSelectionSort(int[] arr)
{
int n = arr.length;
for(int i = 0; i < n - 1; i++)
{
int minIndex = i;
for(int j = i + 1; j < n; j++)
{
if(arr[j] < arr[minIndex])
{
minIndex = j;
}
}
  
int temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
}
  
// print the sorted array
System.out.print("\nThe sorted array is: ");
for(int i = 0; i < arr.length; i++)
{
if(i == arr.length - 1)
System.out.print(arr[i] + "");
else
System.out.print(arr[i] + ", ");
}
System.out.println();
}
}

How many integers you want to enter? 5 Enter the integers: 10 The sorted array is 10, 22, 33, 44, 88 BUILD SUCCESSFUL (total

Add a comment
Know the answer?
Add Answer to:
A. Create a class called StudentRecord that has the following private variables: year, GPA Implem...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Create a java class for an object called Student which contains the following private attributes: a...

    Create a java class for an object called Student which contains the following private attributes: a given name (String), a surname (family name) (String), a student ID number (an 8 digit number, String or int) which does not begin with 0. There should be a constructor that accepts two name parameters (given and family names) and an overloaded constructor accepting two name parameters and a student number. The constructor taking two parameters should assign a random number of 8 digits....

  • Create a class called Student. This class will hold the first name, last name, and test...

    Create a class called Student. This class will hold the first name, last name, and test grades for a student. Use separate files to create the class (.h and .cpp) Private Variables Two strings for the first and last name A float pointer to hold the starting address for an array of grades An integer for the number of grades Constructor This is to be a default constructor It takes as input the first and last name, and the number...

  • C++ There is a class called Person which has name and age as private variables. Another...

    C++ There is a class called Person which has name and age as private variables. Another class called Student, which is derived from person with gpa and id as variables. name is a string, age and id are integers and gpa is a double... All of them are private variables. age, gpa and id should be generated randomly when the object is created with the following ranges: age 20 to 32 gpa 0.0 to 4.0 // this one is tricky...

  • in C++: Create a class named Student that has three member variables: name - string that...

    in C++: Create a class named Student that has three member variables: name - string that stores the name of the student numClasses - integer that tracks how many courses the student is currently enrolled in, this number must be between 1 and 100. classList - an array of strings of size 100 used to store the names of the classes that the student is enrolled in Write the appropriate constructor(s), mutator and accessor functions for the class along with...

  • Write a program that meets the following requirements: Sandwich Class Create a class called Sandwich which has the following instance variables. No other instance variables should be used: - ingredi...

    Write a program that meets the following requirements: Sandwich Class Create a class called Sandwich which has the following instance variables. No other instance variables should be used: - ingredients (an array of up to 10 ingredients, such as ham, capicola, American cheese, lettuce, tomato) -condiments (an array of up to 5 condiments, such as mayonnaise, oil, vinegar and mustard) Create a two argument constructor Write the getters and setters for the instance variables Override the toString method using the...

  • Start a new project in NetBeans that defines a class Person with the following: Instance variables...

    Start a new project in NetBeans that defines a class Person with the following: Instance variables firstName (type String), middleInitial (type char) and lastName (type String) (1) A no-parameter constructor with a call to the this constructor; and (2) a constructor with String, char, String parameters (assign the parameters directly to the instance variables in this constructor--see info regarding the elimination of set methods below) Accessor (get) methods for all three instance variables (no set methods are required which makes...

  • Please help me with this code. Thank you Implement the following Java class: Vehicle Class should...

    Please help me with this code. Thank you Implement the following Java class: Vehicle Class should contain next instance variables: Integer numberOfWheels; Double engineCapacity; Boolean isElectric, String manufacturer; Array of integers productionYears; Supply your class with: Default constructor (which sets all variables to their respective default values) Constructor which accepts all the variables All the appropriate getters and setters (you may skip comments for this methods. Also, make sure that for engineCapacity setter method you check first if the vehicle...

  • In a project named 'DogApplication', create a class called 'Dog' 1. declare two instance variables in...

    In a project named 'DogApplication', create a class called 'Dog' 1. declare two instance variables in 'Dog' : name (String type) age (int type) 2. declare the constructor for the 'Dog' class that takes two input parameters and uses these input parameters to initialize the instance variables 3. create another class called 'Driver' and inside the main method, declare two variables of type 'Dog' and call them 'myDog' and 'yourDog', then assign two variables to two instance of 'Dog' with...

  • Create a class called AddressBook. It should be able to store a maximum of 50 names...

    Create a class called AddressBook. It should be able to store a maximum of 50 names and 50 phone numbers (make an array of structures in the object or two arrays). It should contain a method called addName that will take a name and phone number and add it to the address book. There should be a method called findName that will take a string for the name and return the phone number if found in the list otherwise return...

  • Create a class called Employee that includes three instance variables—a first name, a last name, and...

    Create a class called Employee that includes three instance variables—a first name, a last name, and a monthly salary. Code the default (no-args, empty) constructor. Provide a set and a get method for each instance variable. If the monthly salary is not positive, do not set its value.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT