Consider 2024 Java 2 Weeks 1-6 for Java 1

Midterm Exam

  • You will randomly be assigned a CSV data set.

    • Read 3-4 fields from the CSV file to create a List of objects.

    • Get the objects with the highest/lowest value of an attribute.

    • Sort the List of objects by two attributes

  • You will randomly be assigned a conversion type.

    • Create a JSP to display a form that reads a single input and displays output.

    • Create a servlet that validates input and produces output.

      • Numbers cannot be negative

  • When finished, raise your hand and show the teacher your completed work.

Midterm Exam Review

  • Download the Covid-19 data set as tab-separated values (TSV) since some of the data contains commas.

  • Create a class called MidtermReview. 

  • Create a class called CovidData

  • Create a static List<CovidData>

import java.util.*;

public class MidtermPractice {
    private static List<CovidData> data = new ArrayList<>();
 
}

class CovidData {
    private String countyName;
    private String state;
    private int totalCases;

    public CovidData(String countyName, String state, int totalCases) {
        this.countyName = countyName;
        this.state = state;
        this.totalCases = totalCases;
    }

    public String getCountyName() {
        return countyName;
    }

    public void setCountyName(String countyName) {
        this.countyName = countyName;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public int getTotalCases() {
        return totalCases;
    }

    public void setTotalCases(int totalCases) {
        this.totalCases = totalCases;
    }

    @Override
    public String toString() {
        return "CovidData [countyName=" + countyName + ", state=" + state + ", totalCases="
                + totalCases + "]";
    }
    
    public static Comparator<CovidData> compareTotalCases = Comparator.comparing(CovidData::getTotalCases);

}

Midterm Exam Review

  • Write a main method

  • Call FileInput.readAllLines method.

  • Convert lines in the List<String> to CovidData objects

  • Print the county with the highest total cases

  • Sort the data highest to lowest.

public static void main(String[] args) {
  List<String> lines = FileInput.readAllLines("COVID-19.tsv");
  // lines.forEach(System.out::println);
  for(String line: lines) {
    try {
      String[] record = line.split("\t");
      String countyName = record[1];
      String state = record[2];
      int totalCases = Integer.parseInt(record[4]);
      data.add(new CovidData(countyName, state, totalCases));
    } catch(NumberFormatException e) {
    	continue;
  	}
  }
  // data.forEach(System.out::println);
  CovidData highest = data.stream().max(Comparator.comparing(CovidData::getTotalCases)).get();
  System.out.println(highest);
  Collections.sort(data, CovidData.compareTotalCases.reversed());
  for(int i = 0; i < 10 ; i++) {
  	System.out.println(data.get(i));
  }
}

Java 2 Week 7

By Marc Hauschildt

Java 2 Week 7

  • 314