public static void main(String[ ] args) {
ArrayList<Person> col =
new ArrayList< >(500);
Person p1 = new Person("Alice", 'F', 21);
Person p2 = new Person("Barry", 'M', 25);
Person p3 = new Person("Chloe", 'F', 19);
col.add(p1);
col.add(p2);
col.add(p3);
System.out.println(col);
}
Alice,F,21 Barry,M,25 Chloe,F,19 Danielle,F,27Here is the main method. It was modified to use a try/catch block (see the Catching Exceptions section below). We used the IntelliJ shortcut Alt-Insert to write the try/catch block:
public static void main(String[ ] args) {
File f = new File("persons2.txt");
Scanner s = null;
try {
s = new Scanner(f);
}
catch (FileNotFoundException e) {
System.out.println("File not found.");
System.exit(1);
}
ArrayList<Person> col = new ArrayList< >(50);
while(s.hasNextLine( )) {
String line = s.nextLine( );
String[ ] fields = line.split(",");
String name = fields[0].strip( );
char gender = fields[1].strip( ).charAt(0);
int age = Integer.parseInt(fields[2].strip( ));
Person p = new Person(name, gender, age);
col.add(p);
}
s.close( );
System.out.println(col);
}
String chars = " ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
String[ ] codes = {" ", ".-", "-...", "-.-.", "-..",
".", "..-.", "--.", "....", "..", ".---", "-.-",
".-..", "--", "-.", "---", ".--.", "--.-", ".-.",
"...", "-", "..-", "...-", ".--", "-..-", "-.--",
"--..", ".----", "..---", "...--", "....-", ".....",
"-....", "--...", "---..", "----.", "-----"};
Write a method named toMorseCode to perform the translation.32 19 63 86
ArrayList<int> col = new ArrayList<int>(500);use
ArrayList<Integer> col = new ArrayList<Integer>(500);where Integer is the wrapper class for an int value.
ArrayList<Integer> col = new ArrayList< >(500);
| Primitive Type | Wrapper Class |
|---|---|
| int | Integer |
| double | Double |
| char | Character |
| boolean | Boolean |
col.add(5);which the compiler converts to this:
col.add(new Integer(5));
try {
int x = 3 / 0;
System.out.println("Quotient == " + x);
}
catch(ArithmeticException e) {
System.out.println("Division by zero.");
}
java.util javax.swing it313.smith edu.depaul.it313.sjost
package it313.smith;
public class Main {
...
}
import java.util.Scanner;allows a Scanner object to be created as
Scanner s = new Scanner(System.in);
java.util.Scanner s = new java.util.Scanner(System.in);
package it313.sjost;Here are the Greeter.java and Main.java files:
// ***************************************************************
// Source code file: Greeter.java
package it313.sjost;
public class Greeter {
public static String makeGreeting(String name) {
return String.format("Hello, %s, how are you?", name);
}
}
// **************************************************************
// Source code file: Main.java
import it313.sjost.Greeter;
public class Main {
public static void main(String[ ] args) {
System.out.println(Greeter.makeGreeting("Larry"));
}
}
Notice that the import statement in Main.java allows the
Main class to use
the Greeter class without qualification. Without the
import statement,
the Greeter.makeGreeting method is called like this:
System.out.println(it313.sjost.Greeter.makeGreeting("Larry"));
Person ← Employee ← ExecutiveAn arrow in a UML diagram means "inherits from."
Employee Manager GraduateStudent Student Person Student Vehicle Car Car Minivan Truck Pickup
| Base Class | Derived Class |
| Parent Class | Child Class |
| Super Class | Subclass |
Student SeminarSpeaker Professor Person TeachingAssistant Course Employee Seminar Secretary Lecture DepartmentChair ComputerLab Janitor
java -jf person.jar person.class unit.class