Question

In: Computer Science

Instructions:DevelopaprogramnamedMakeStudentsthat  Reads student data from Readme.txt and creates student instances o usetheStudentclassprovidedforthelab  Saves student...

Instructions:DevelopaprogramnamedMakeStudentsthat

  •  Reads student data from Readme.txt and creates student instances

    o usetheStudentclassprovidedforthelab

  •  Saves student instances in an array list

  •  Displays the size of the list

  •  Iterates through the list displaying students one student per line, as in:

    for (Student s: myList) { System.out.println(s);

    }

    new File("Readme.txt")).useDelimiter(",");

    Then to get each token we can use f.next() or f.nextBoolean()
    Note that student gender is of type char, and that char value can be obtained using

    f.next().charAt(0)

    YoumustusetheStudentclassgivenalongwiththislab. ThisversionofStudentissimpler than the one in the text – this version makes no reference to the Subject class (not needed for this lab).

    Your BlueJ project will include

    Readme.txt, Student.java, and MakeStudents.java

    SubmitMakeStudents.javatotheemailcorrespondingtoyourlabsectionwithsubject Lab 10

Copy the student data provided for this lab to Readme.txt. The data comprises comma-

separated values for fields: first name, last name, gender and active.

Previously we have used the Scanner class with its default delimiter of whitespace. In this

lab, use the Scanner class with comma as the delimiter:

Scanner f = new Scanner

------------------------------------------------------

public class Student {

// class fields

private static int lastId;

// instance fields

private int id;

private String firstName;

private String lastName;

private char gender;

private boolean active;

// first constructor, no arguments

public Student(){

id = nextId();

// default values for a student:

firstName = "unknown";

lastName = "unknown";

gender = '?';

active = false;

}

// second constructor, four arguments

public Student (String firstName, String lastName, char gender, boolean active){

id = nextId();

//

// when parameters and fields have the same

// name they are distinquished this way:

// a field name alone refers to the parameter

// a field name prefixed with "this."

// refers to an object's fields.

this.firstName = firstName;

this.lastName = lastName;

this.gender = gender;

this.active = active;

}

private int nextId(){

// increment lastId and return the new value

// to be used for the new student.

return ++lastId;

}

public int getId(){

return id;

}

public static int getLastId(){

return lastId;

}

public String getFirstName(){

return firstName;

}

public String getLastName(){

return lastName;

}

public char getGender(){

return gender;

}

public boolean isActive(){

return active;

}

public void setLastId(int newLastId){

lastId = newLastId;

}

// no setter for the student's id field

// public void setId(int newId){

// id = newId;

// }

public void setFirstName(String newFirstName){

firstName = newFirstName;

}

public void setLastName(String newLastName){

lastName = newLastName;

}

public void setGender(char newGender){

gender = newGender;

}

public void setActive(boolean newActive){

active = newActive;

}

public String toString(){

return id+" "+firstName+" "+lastName;

}

public boolean equals(Student s){

return id == s.id;

}

}

---------------------------------------------------------

Readme.txt

Harry,Potter,m,true,Albus,Dumbledore,m,true,Serverus,Snape,m,true,Rubeus,Sirius,m,true,Hermione,Granger,f,true,Ron,Weasley,m,true,Draco,Malfoy,m,true,Luna,Lovegood,f,true,Regulus,Black,m,true,Neville,Longbottom,m,true,Nymphadora,Tonks,f,true,Remus,Lupin,m,true,Fleur,Delacour,f,true,Dolores,Umbridge,f,true,Gellert,Grindelwald,m,true

Solutions

Expert Solution

A) Program for the above question is given below. Name the file name as MakeStudents.java. The other files Student.java and Readme.txt should be in the same folder.

Explanation:
The student data is read form Readme.txt using Scanner class as specified with comma as the delimiter and student instances are created using the Student class provided. The Student instances are stored in an ArrayList as specified. Later the size of the list is printed and the ArrayList is iterated and the students one per line are also printed.

Code:

import java.util.*;
import java.io.File;
import java.io.IOException;

public class MakeStudents {
   public static void main(String[] args)
{
   ArrayList<Student> studentList = new ArrayList<Student>();
   try {

       Scanner sc = new Scanner(new File("Readme.txt")).useDelimiter(",");
       while (sc.hasNext()) {
           String firstName = sc.next();
               String lastName = sc.next();
               char gender = sc.next().charAt(0);
               boolean active = sc.nextBoolean();

               Student student = new Student(firstName, lastName, gender, active);
               studentList.add(student);
       }
       sc.close();
   } catch (IOException e) {
       e.printStackTrace();
   }
   System.out.println("Size of the list: " + studentList.size());
   for (Student s: studentList) {
       System.out.println(s);
   }
}
}

Please refer to the screenshot of the code to understand the indentation of the code

Output: The output is given for the Readme.txt file provided.

import java.util.*; import java.io.File; import java.io.IOException; public class MakeStudents { public static void main(String[] args) ArrayList<Student> studentList = new ArrayList<Student>(); try {l Scanner sc = new Scanner(new File("Readme.txt")).useDelimiter(","); while (sc.hasNext()) { String firstName = sc.next(); String lastName = sc.next(); char gender = sc.next().charAt(@); boolean active = sc.nextBoolean(); Student student = new Student(firstName, lastName, gender, active); studentList.add(student); sc.close(); '} catch (IOException e) { e.printStackTrace(); System.out.println("Size of the list: " + studentList.size()); for (Student s: studentList) { System.out.println(s);

Size of the list: 7 1 Harry Potter 2 Albus Dumbledore 3 Serverus Snape 4 Rubeus Sirius 5 Hermione Granger 6 Ron Weasley 7 Draco Malfoy


Related Solutions

Specification This script reads in class data from a file and creates a dictionary. It then...
Specification This script reads in class data from a file and creates a dictionary. It then prints the data from the dictionary. The script has the following functions class_tuple_create print_classes class_tuple_create This function has the following header def class_tuple_create(filename): The function reads in a file with four fields of data about a class. The fields are Course ID Room number Instructor Start time The function must print an error message if the file cannot be opened for any reason. The...
In this assignment you will write a PHP program that reads from a data file, creates...
In this assignment you will write a PHP program that reads from a data file, creates an associative array, sorts the data by key and displays the information in an HTML table. Create a PHP file called hw4.php that will do the following: - Display your name. - Read text data from a file. The data file is hw3.txt. The file hw3.txt will hold the following text: PQRParrot, Quagga, Raccoon DEFDo statements, Else statements, For statements GHIGeese, Hippos, If statements...
in. java Write a program that reads a string from the user, and creates another string...
in. java Write a program that reads a string from the user, and creates another string with the letters from in reversed order. You should do this using string concatenation and loops. Do not use any method from Java that does the work for you. The reverse string must be built. Do not just print the letters in reversed order. Instead, concatenate them in a string. --- Sample run: This program will create a string with letters in reversed order....
Create a program that: Creates a sales receipt, displays the receipt entries and totals, and saves...
Create a program that: Creates a sales receipt, displays the receipt entries and totals, and saves the receipt entries to a file Prompt the user to enter the Item Name Item Quantity Item Price Display the item name, the quantity, and item price, and the extended price (Item Quantity multiplied by Item Price) after the entry is made Save the item name, quantity, item price, and extended price to a file When you create the file, prompt the user for...
Create a C++ program that creates instances of a Struct using an Array (you can choose...
Create a C++ program that creates instances of a Struct using an Array (you can choose the number of instances to create). You can also use either user input or an initialization list to initialize 3 peoples names. Make sure that the values stored in each member are printed to the screen.
Write a program, which reads a list of student records from a file in batch mode....
Write a program, which reads a list of student records from a file in batch mode. Each student record comprises a roll number and student name, and the student records are separated by commas. An example data in the file is given below. · SP18-BCS-050 (Ali Hussan Butt), SP19-BCS-154 (Huma Khalid), FA19-BSE-111 (Muhammad Asim Ali), SP20-BSE-090 (Muhammad Wajid), SP17-BCS-014 (Adil Jameel) The program should store students roll numbers and names separately in 2 parallel arrays named names and rolls, i.e....
A data set includes data from student evaluations of courses.
A data set includes data from student evaluations of courses. The summary statistics are n = 89, x̅= 3.34, s=0.51. Use a 0.10 significance level to test the claim that the population of student course evaluations has a mean equal to 3.50. Assume that a simple random sample has been selected. Identify the null and alternative hypotheses, test statistic, P-value, and state the final conclusion that addresses the original claimWhat are the null and alternative hypotheses?Determine the test statistic (Round...
Create a program that creates a sorted list from a data file. The program will prompt...
Create a program that creates a sorted list from a data file. The program will prompt the user for the name of the data file. Create a class object called group that contains a First Name, Last Name, and Age. Your main() function should declare an array of up to 20 group objects, and load each line from the input file into an object in the array. The group class should have the following private data elements: first name ,last...
c++ Create a program that creates a sorted list from a data file. The program will...
c++ Create a program that creates a sorted list from a data file. The program will prompt the user for the name of the data file. Create a class object called group that contains a First Name, Last Name, and Age. Your main() function should declare an array of up to 20 group objects, and load each line from the input file into an object in the array. The group class should have the following private data elements: first name...
A student creates 150ml of a buffer that is .018M in formic acid and .016M in...
A student creates 150ml of a buffer that is .018M in formic acid and .016M in sodium formate. What would the pH of the buffer be after the addition 15ml of .070M HCl solution to the buffer? a) 3.69 b) 3.85 c) 4.18 d) 1.15 e) 3.30
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT