JAVA PROGRAMMING
In this final review lab from our intro course, use the Name class you created in the previous lab.
In this lab, create a Student class with the following class variable:
Create the getter and setter methods. In addition to these methods, create a toString() method, which overrides the object class toString() method. This override method prints the current value of any of this class object.
Complete the main() method below to test your project.
public static void main(String[] args)
List<Studcnt> myArrayList = new ArrayList<Student>();
---------------------------------------
--------------------------------------
--------------------------------------
for(Studnct students: myArrayList) {
System.out.println(students);
}
Here is name.java
public class Name
{
//Variable declaration
String firstName, lastName, middleName, fullName;
//Class constructor
public Name(String firstName, String lastName, String middleName)
{
this.firstName = firstName;
this.lastName = lastName;
this.middleName = middleName;
this.fullName = firstName +" "+middleName+" "+lastName;
}
//getter method to get first name
public String getFirstName() {
return firstName;
}
//Setter Method
public void setFirstName(String firstName) {
this.firstName = firstName;
}
//getter method to get last name
public String getLastName() {
return lastName;
}
//Setter Method for last name
public void setLastName(String lastName) {
this.lastName = lastName;
}
//getter method to get middle name
public String getMiddleName() {
return middleName;
}
//Setter Method for middle name
public void setMiddleName(String middleName) {
this.middleName = middleName;
}
//getter method to get full name
public String getFullName() {
return fullName;
}
//Setter Method for full name
public void setFullName(String fullName) {
this.fullName = fullName;
}
//toString method
public String toString() {
return "Name{" +
"fullName='" + fullName + '\'' +
'}';
}
package shop;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Test{
public static void main(String[] args) {
Scanner sc=new
Scanner(System.in);
Student s;
Name n;
List<Student> myArrayList =
new ArrayList<Student>();
//first ask user to input name and
id
System.out.print("Please enter
first name:");
String fname=sc.next();
System.out.print("Please enter
middle name:");
String mname=sc.next();
System.out.print("Please enter last
name:");
String lname=sc.next();
System.out.print("Please enter
Student Id:");
String id=sc.next();
n=new Name(fname,mname,lname);
//store it name and student object
s=new Student(n,id);
myArrayList.add(s); //add to
list
for(Student students: myArrayList)
{ //print list
System.out.println(students);
}
}
}
public class Student {
Name name;
String student_id;
//Constructor to set name and id
public Student(Name n, String id) {
this.name=n;
this.student_id=id;
}
//getters setters for above two variable
public Name getName() {
return name;
}
public void setName(Name name) {
this.name = name;
}
public String getStudent_id() {
return student_id;
}
public void setStudent_id(String student_id)
{
this.student_id = student_id;
}
//toString method to print full name and id
public String toString(){
String res="Full Name: ";
res+=name.getFullName()+" Id:
"+student_id;
return res;
}
}
public class Name
{
//Variable declaration
String firstName, lastName, middleName, fullName;
//Class constructor
public Name(String firstName, String lastName, String middleName)
{
this.firstName = firstName;
this.lastName = lastName;
this.middleName = middleName;
this.fullName = firstName +" "+middleName+" "+lastName;
}
//getter method to get first name
public String getFirstName() {
return firstName;
}
//Setter Method
public void setFirstName(String firstName) {
this.firstName = firstName;
}
//getter method to get last name
public String getLastName() {
return lastName;
}
//Setter Method for last name
public void setLastName(String lastName) {
this.lastName = lastName;
}
//getter method to get middle name
public String getMiddleName() {
return middleName;
}
//Setter Method for middle name
public void setMiddleName(String middleName) {
this.middleName = middleName;
}
//getter method to get full name
public String getFullName() {
return fullName;
}
//Setter Method for full name
public void setFullName(String fullName) {
this.fullName = fullName;
}
//toString method
public String toString() {
return "Name{" +
"fullName='" + fullName + '\'' +
'}';
}
}
Please enter first name:Monica
Please enter middle name:Fallula
Please enter last name:Geller
Please enter Student Id:5
Full Name: Monica Geller Fallula Id: 5
JAVA PROGRAMMING In this final review lab from our intro course, use the Name class you...
Exercise 1 • Examine ShoppingCart.java. – Perform the following: – Use the indexOf method to get the index for the space character (" ") within custName. Assign it to spaceIdx. – Use the substring method and spaceIdx to get the first name portion of custName. Assign it to firstName and print firstName. public class ShoppingCart { public static void main (String[] args){ String custName = "Steve Smith"; String firstName; int spaceIdx; // Get the...
I have a little problem about my code. when i'm working on "toString method" in "Course" class, it always say there is a mistake, but i can not fix it: Student class: public class Student { private String firstName; private String lastName; private String id; private boolean tuitionPaid; public Student(String firstName, String lastName, String id, boolean tuitionPaid) { this.firstName=firstName; this.lastName=lastName; this.id=id; this.tuitionPaid=tuitionPaid; } ...
Introduction In this lab, you will be working with three classes: Person, Student, and Roster. Person and Student represent individuals with a first and last name as String class variables, and Student has an additional int class variable that represents a ID number. The Roster class represents a group of people that are objects of either the Person or Student class. It contains an ArrayList. The Person class has been completed for you with class variables, a constructor, and a...
Language Java Step 1: Design a class called Student. The Student class should contain the following data: firstName lastName studentID totalCredits gpa Your class should implement the “sets” and “gets” for the class. Include methods: equals, compareTo, toString. Change the toString method (from class) to put each member variable on a new line. Step 2: Write a driver program to test that Student works correctly. Test is with 3 students as given in class. Step 3: Write a “registration” program...
I have currently a functional Java progam with a gui. Its a
simple table of contacts with 3 buttons: add, remove, and edit.
Right now the buttons are in the program but they do not work yet.
I need the buttons to actually be able to add, remove, or edit
things on the table. Thanks so much.
Here is the working code so far:
//PersonTableModel.java
import java.util.List;
import javax.swing.table.AbstractTableModel;
public class PersonTableModel extends AbstractTableModel
{
private static final int...
What is wrong with this Code? Fix the code errors to run correctly without error. There are four parts for one code, all are small code segments for one question. public class StudentTest { public static void main(String[] args){ // Part Time Student Test Student ft1 = new PartTimeStudent("George", "Bush", "123-12-5678", 1, 2 ); System.out.println(ft1); Student ft2 = new PartTimeStudent("Abraham", "Lincoln", "001-90-5323", 6, 22 ); System.out.println(ft2); // Full Time Student Test Student pt1 = new FullTimeStudent("Nikola", "Tesla", "442-00-0998", 8, 26...
FOR JAVA: Summary: Create a program that adds students to the class list (see below). The solution should be named Roster402_v2.java. Allow the user to control the number of students added to the roster. Ask if the user would like to see their new roster to confirm additions. If yes, then display contents of the file, if no, end the program. ------------------------------------------------------------------------------------- List of student names and IDs for class (this will be your separate text file): Jones, Jim,45 Hicks,...
Java Homework Problems: 4. • Examine AddImport.java. – Perform the following: – Replace the fully qualified name to access the Jlabel component with an import statement. – To import classes from the util package, replace multiple import statements with a single import statement. /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ import java.util.Calendar; import java.util.Date; public...
Given the following Student class, write an equals method to determine if two instances of Student are the same person. The equals method should base it's comparison on the student's M number. public class Student { String firstName = ""; String lastName = ""; String mNumber = ""; Student(String firstName, String lastName, String mNumber) { this.firstName = firstName; this.lastName = lastName; this.mNumber = mNumber; } private String getMNumber() { return mNumber; } <other methods are imlemented here> }
8.26
Encapsulate the Name class. Modify the existing code shown below
to make its fields private, and add appropriate accessor methods to
the class named getFirstName, getMiddleInitial, and
getLastName.
code:
class Name {
private String firstName;
private String middleNames;
private String lastName;
//Constructor method
public Name(String firstName, String middleNames, String
lastName)
{
this.firstName = firstName;
this.middleNames = middleNames;
this.lastName = lastName;
}
//Accessor for firstName
public String getFirstName()
{
return firstName;
}
//Accessor for middleNames
public String getMiddlesNames()...