Compare commits

...

2 Commits

Author SHA1 Message Date
ed696f9493 Add final java project 2021-04-02 15:29:34 -04:00
307d7d1876 Update javascript class 2021-04-02 15:29:15 -04:00
13 changed files with 10972 additions and 0 deletions

View File

@ -0,0 +1,70 @@
"use strict"
class SuperHuman {
constructor(name, powerLevel) {
this.name = name;
this.powerLevel = powerLevel;
}
}
// Define SuperHero and SuperVillain classes here
class SuperHero extends SuperHuman{
constructor(name, alias, powerLevel){
super(name, powerLevel);
this.alias = alias;
}
battle(sv){
if (this.powerLevel >= sv.powerLevel){return true;}
else{return false;}
}
}
class SuperVillain extends SuperHuman{
constructor(name, alias, powerLevel){
super(name, powerLevel);
this.alias = alias;
}
getEvilChuckle(){return "Ha ha ha!";}
}
const heroes = {
"Super Bacon": new SuperHero("Jack Oinker", "Super Bacon", 2),
"Flat-Man": new SuperHero("Peter Pranker", "Flat-Man", 5),
"Mighty Woman": new SuperHero("Diana Dense", "Mighty Woman", 8),
"Captain Marvelous": new SuperHero("Carol Hangers", "Captain Marvelous", 9)
}
const villains = {
"The Jokester": new SuperHero("Jack Nastier", "The Jokester", 3),
"Magnet Man": new SuperHero("Max Eisenhardt", "Magnet Man", 6),
"Lex Loner": new SuperHero("Lex Loner", "Lex Loner", 2),
"Thankos": new SuperHero("Thankos", "Thankos", 9)
}
window.addEventListener("DOMContentLoaded", domLoaded);
function domLoaded() {
// Detect selection of hero and villain
document.querySelector("#heroSelect").addEventListener("change", selectionChanged);
document.querySelector("#villainSelect").addEventListener("change", selectionChanged);
selectionChanged();
}
function selectionChanged() {
var selectedHeroValue = document.querySelector("#heroSelect").value;
var selectedVillainValue = document.querySelector("#villainSelect").value;
// Get hero and villain selected
var selectedHero = heroes[selectedHeroValue];
var selectedVillain = villains[selectedVillainValue];
// Your code goes here
if (selectedHero.battle(selectedVillain)){
document.querySelector("#winner").innerHTML = "Winner: " + selectedHero.alias + "!";
}
else{
document.querySelector("#winner").innerHTML = "Winner: " + selectedVillain.alias + "!";
}
}

View File

@ -0,0 +1,33 @@
<!DOCTYPE html>
<html lang="en">
<title>Super Heroes vs. Super Villains</title>
<script src="hero.js"></script>
<style>
body {
font-family: Arial, Helvetica, sans-serif;
}
</style>
<body>
<h1>Super Heroes vs. Super Villains</h1>
<p>Select a hero:
<select id="heroSelect">
<option>Super Bacon</option>
<option>Flat-Man</option>
<option>Mighty Woman</option>
<option>Captain Marvelous</option>
</select>
</p>
<p>Select a villain:
<select id="villainSelect">
<option>The Jokester</option>
<option>Magnet Man</option>
<option>Lex Loner</option>
<option>Thankos</option>
</select>
</p>
<p id="winner"></p>
</body>
</html>

View File

@ -0,0 +1,88 @@
"use strict"
let groceryList = [];
// Wait until DOM is loaded
window.addEventListener("DOMContentLoaded", function() {
document.querySelector("#addBtn").addEventListener("click", addBtnClick);
document.querySelector("#clearBtn").addEventListener("click", clearBtnClick);
// Load the grocery list from localStorage
groceryList = loadList();
if (groceryList.length > 0) {
// Display list
for (let item of groceryList) {
showItem(item);
}
}
else {
// No list to display
enableClearButton(false);
}
});
// Enable or disable the Clear button
function enableClearButton(enabled) {
document.querySelector("#clearBtn").disabled = !enabled;
}
// Show item at end of the ordered list
function showItem(item) {
let list = document.querySelector("ol");
list.innerHTML += `<li>${item}</li>`;
}
// Add item to grocery list
function addBtnClick() {
let itemTextInput = document.querySelector("#item");
let item = itemTextInput.value.trim();
if (item.length > 0) {
enableClearButton(true);
showItem(item);
groceryList.push(item);
// Save groceryList to localStorage
saveList(groceryList);
}
// Clear input
itemTextInput.value = '';
}
// Clear the list
function clearBtnClick() {
enableClearButton(false);
groceryList = [];
let list = document.querySelector("ol");
list.innerHTML = "";
// Remove the grocery list from localStorage
clearList();
}
// Complete the functions below
function loadList() {
let groceryList;
let storage;
groceryList = [];
storage=localStorage.getItem("list");
if (storage != null){
groceryList = storage.split(",");
}
return groceryList;
}
function saveList(groceryList) {
console.log(groceryList.toString());
localStorage.setItem("list", groceryList.toString());
}
function clearList() {
localStorage.clear();
}

View File

@ -0,0 +1,17 @@
<!DOCTYPE HTML>
<html lang="en">
<title>Grocery List</title>
<style>
body {
font-family: Arial, Helvetica, sans-serif;
}
</style>
<script src="groceries.js"></script>
<body>
<h1>Grocery List</h1>
<input type="text" id="item"> <input type="button" id="addBtn" value="Add">
<ol></ol>
<input type="button" id="clearBtn" value="Clear">
</body>
</html>

View File

@ -0,0 +1,42 @@
import java.util.ArrayList;
import java.util.Scanner;
import java.io.FileInputStream;
//time 11:10 - 11:22
public class CovidDatabase {
private ArrayList<CovidEntry> list;
public CovidDatabase(){
list = new ArrayList<CovidEntry>();
}
public void readCovidData(String filename){
Scanner scnr = null;
FileInputStream fs = null;
try{
fs = new FileInputStream(filename);
scnr = new Scanner(fs);
scnr.useDelimiter("[,\r\n]+");
scnr.nextLine();
while(scnr.hasNext()){
String state = scnr.next();
int day = scnr.nextInt();
int month = scnr.nextInt();
int di = scnr.nextInt();
int dd = scnr.nextInt();
int ti = scnr.nextInt();
int td = scnr.nextInt();
CovidEntry c = new CovidEntry(state,
month, day, di, dd, ti, td);
list.add(c);
}
fs.close();
}
catch(Exception e){
System.out.println("File error with " + filename);
}
}
}

View File

@ -0,0 +1,47 @@
import java.text.DecimalFormat;
public class CovidEntry {
//time1 - 10:10 10:30
//Private vars
private String state;
private int month, day;
private int dailyDeaths, dailyInfections;
private int totalDeaths, totalInfections;
private static DecimalFormat df;
//Constructor
public CovidEntry(String st, int m, int d, int di, int dd,
int ti, int td){
this.state = st;
this.month = m;
this.day = d;
this.dailyInfections = di;
this.dailyDeaths = dd;
this.totalDeaths = td;
this.totalInfections = ti;
df = new DecimalFormat("#,###,###");
}
//accessors
public int getMonth(){return month;}
public int getDay(){return day;}
public String getState(){return state;}
public int getDailyInfections(){return dailyInfections;}
public int getDailyDeaths(){return dailyDeaths;}
public int getTotalDeaths(){return totalDeaths;}
public int getTotalInfections(){return totalInfections;}
public String toString(){
return state + " " + month + "/" + day + " " +
df.format(dailyInfections) + " infections, " +
df.format(dailyDeaths) + " deaths";
}
}

View File

@ -0,0 +1,43 @@
import static org.junit.Assert.*;
import org.junit.*;
/*******************************************
* Class to test the CovidEntry class
*
* @author -
* @version -
******************************************/
public class CovidEntryJUnit{
/** object of the CovidEntry class*/
private CovidEntry covid;
/******************************************************
* Test constructor
*****************************************************/
@Test
public void testConstructor() {
//Testing a female baby name
covid = new CovidEntry("MI",8,1,758,7,91332,6457);
assertEquals("Constructor: State should be equal to value of input parameter",
"MI", covid.getState());
assertEquals("Constructor: month should be equal to value of input parameter",
8, covid.getMonth());
assertEquals("Constructor: day should be equal to value of input parameter",
1, covid.getDay());
assertEquals("Constructor: daily infections should be equal to value of input parameter",
758, covid.getDailyInfections());
assertEquals("Constructor: daily deaths should be equal to value of input parameter",
7, covid.getDailyDeaths());
assertEquals("Constructor: total infections should be equal to value of input parameter",
91332, covid.getTotalInfections());
assertEquals("Constructor: total deaths should be equal to value of input parameter",
6457, covid.getTotalDeaths());
}
}

View File

@ -0,0 +1,18 @@
public class CovidManager {
public static void main( String[] args){
CovidEntry covidObject =
new CovidEntry("TX",
4, 12,
233343,
5,
10,
15);
System.out.println(covidObject);
CovidDatabase db = new CovidDatabase();
db.readCovidData("data/covid_data.csv");
System.out.println("");
}
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,267 @@
import static org.junit.Assert.*;
import java.util.ArrayList;
import org.junit.*;
/*******************************************
* Class to test the CovidDatabase project
*
* @author -
* @version -
******************************************/
public class CovidDatabaseJUnit{
/** object of the CovidDatabase class */
private CovidDatabase database;
/******************************************************
* Test constructor
*****************************************************/
@Test
public void testConstructor()
{
database= new CovidDatabase();
assertEquals("ArrayList should not contain any records at this time",
0, database.countRecords ());
}
/******************************************************
* Test read file and counts
*****************************************************/
@Test
public void testReadFileAndCounts()
{
database= new CovidDatabase();
database.readCovidData("covid_data.csv");
assertEquals("ArrayList should not contain 10346 records",
10346, database.countRecords ());
}
/******************************************************
* Test statics for all the covid entries
*****************************************************/
@Test
public void testStatistics()
{
database= new CovidDatabase();
database.readCovidData("covid_data.csv");
// testing total deaths
assertEquals("Total deaths up to 9/27 should be 196,696",
196696, database.getTotalDeaths());
// testing total infections
assertEquals("Total infections up to 9/27 should be 7,032,090",
7032090, database.getTotalInfections());
// testing most total infections
CovidEntry c = database.mostTotalDeaths();
assertEquals("State with most total deaths up to 9/27 is NY",
"NY", c.getState());
assertEquals("Number of deaths in NY up to 9/27: 25,456" ,
25456, c.getTotalDeaths());
}
/******************************************************
* Test peak daily deaths
*****************************************************/
@Test
public void testPeakDailyDeaths()
{
database= new CovidDatabase();
database.readCovidData("covid_data.csv");
CovidEntry c;
// testing peak daily deaths for a date 8/12
c = database.peakDailyDeaths(8, 12);
assertEquals("State with peak deaths on 8/12 is TX",
"TX", c.getState());
assertEquals("Number deaths in TX on 8/12: 324" ,
324, c.getDailyDeaths());
// testing peak daily deaths for a state
c = database.peakDailyDeaths("MI");
assertEquals("Peak deaths up to 9/27 in MI",
"MI", c.getState());
assertEquals("Peak number of deaths for MI: 169" ,
169, c.getDailyDeaths());
// testing peak daily deaths for a date 4/30
c = database.peakDailyDeaths(4, 30);
assertEquals("State with peak deaths on 4/30 is NJ",
"NJ", c.getState());
assertEquals("Number deaths in NJ on 4/30: 481" ,
481, c.getDailyDeaths());
// testing peak daily deaths for a state
c = database.peakDailyDeaths("NY");
assertEquals("Peak deaths up to 9/27 in NY",
"NY", c.getState());
assertEquals("Peak number of deaths for NY: 951" ,
951, c.getDailyDeaths());
}
/******************************************************
* Test peak daily deaths - invalid state & invalid date
*****************************************************/
@Test
public void testPeakDailyDeathsInvalidParameters()
{
database= new CovidDatabase();
database.readCovidData("covid_data.csv");
CovidEntry c;
// testing peak daily deaths for an invalid date
c = database.peakDailyDeaths(13, 12);
assertEquals("Wrong date 13/12 should return null",
null, c);
// testing peak daily deaths for an invalid state
c = database.peakDailyDeaths("XX");
assertEquals("Wrong state XX should return null" ,
null, c);
// testing peak daily deaths for date not found in the file
c = database.peakDailyDeaths(1, 1);
assertEquals("No records for 1/1 should return null",
null, c);
}
/******************************************************
* Test safe to open
*****************************************************/
@Test
public void testSafeToOpen() {
ArrayList<CovidEntry> list;
database= new CovidDatabase();
database.readCovidData("covid_data.csv");
// testing safe to open WA
list = database.safeToOpen("WA");
assertEquals("Safe to open should have 5 records" ,
5, list.size());
//testing first record on the list
assertEquals("The states should be WA",
"WA", list.get(0).getState());
assertEquals("Month in the first record in the sequence should be 5",
5, list.get(0).getMonth());
assertEquals("Day in the first record in the sequence should be 6",
6, list.get(0).getDay());
assertEquals("Infections in the first record in the sequence whould be 289",
289, list.get(0).getDailyInfections());
//testing last record on the list
assertEquals("The states should be WA",
"WA", list.get(4).getState());
assertEquals("Month in the lasst record in the sequence should be 5",
5, list.get(4).getMonth());
assertEquals("Day in the last record in the sequence should be 10",
10, list.get(4).getDay());
assertEquals("Infections in the last record in the sequence whould be 62",
62, list.get(4).getDailyInfections());
// test safe to open - California
list = database.safeToOpen("CA");
assertEquals("Safe to open should have 5 records" ,
5, list.size());
//testing first record on the list
assertEquals("The states should be CA",
"CA", list.get(0).getState());
assertEquals("Month in the first record in the sequence should be 5\4",
4, list.get(0).getMonth());
assertEquals("Day in the first record in the sequence should be 21",
21, list.get(0).getDay());
assertEquals("Infections in the first record in the sequence whould be 2283",
2283, list.get(0).getDailyInfections());
//testing last record on the list
assertEquals("The states should be CA",
"CA", list.get(4).getState());
assertEquals("Month in the lasst record in the sequence should be 4",
4, list.get(4).getMonth());
assertEquals("Day in the last record in the sequence should be 25",
25, list.get(4).getDay());
assertEquals("Infections in the last record in the sequence whould be 1883",
1883, list.get(4).getDailyInfections());
// testing a state not found in the database
list = database.safeToOpen("ZZ");
assertEquals("State not valid - safeToOpen should return null" ,
null, list);
}
/******************************************************
* Test minimum infections on a date
*****************************************************/
@Test
public void testMinInfections() {
ArrayList<CovidEntry> list;
database= new CovidDatabase();
database.readCovidData("covid_data.csv");
// testing a Minimum of 2,500 infections on 9/11
list = database.listMinimumDailyInfections(9, 11, 2500);
assertEquals("three records with 2,500 infections or more on 9/11" ,
3, list.size());
//testing highest - index 0
assertEquals("TX should have the highest number of deaths on 8/1",
"TX", list.get(0).getState());
assertEquals("Infections in TX on 9/11 should be 3547",
3547, list.get(0).getDailyInfections());
//testing last entry
assertEquals("CA should be the last entry on the list",
"CA", list.get(2).getState());
assertEquals("Infections in CA on 9/11 should be 3326",
3326, list.get(2).getDailyInfections());
// testing parameter values not found in database
list = database.listMinimumDailyInfections(9, 11, 6000);
assertEquals("Infections of 6,000 or more on 9/11 hould be zero" ,
0, list.size());
// testing wrong parameter values
list = database.listMinimumDailyInfections(13, 11, 10);
assertEquals("Invalid date 13/11 Infections of 10 or more on 9/11 hould be zero" ,
0, list.size());
}
/******************************************************
* Test top ten
*****************************************************/
@Test
public void testTopTen() {
ArrayList<CovidEntry> list;
database= new CovidDatabase();
database.readCovidData("covid_data.csv");
// testing top ten for 8/1
list = database.topTenDeaths(8, 1);
assertEquals("top ten number of records should be 10" ,
10, list.size());
//testing highest - index 0
assertEquals("TX should have the highest number of deaths on 8/1",
"TX", list.get(0).getState());
assertEquals("Deaths in TX on 8/1 should be 268",
268, list.get(0).getDailyDeaths());
//testing CovidEntry at index 6
assertEquals("NC should be at index 6 of the list of deaths on 8/1",
"NC", list.get(6).getState());
assertEquals("Deaths in NC on 8/1 should be 40",
40, list.get(6).getDailyDeaths());
// testing a date not found in the database
list = database.topTenDeaths(10, 31);
assertEquals("top ten number of records should be zero" ,
0, list.size());
// testing an invalid date
list = database.topTenDeaths(-10, 31);
assertEquals("top ten number of records should be zero" ,
0, list.size());
}
}