Objects-Instances of classes
- Class - a blueprint for creating objects with same behavior and defined attributes
- Object - specific entry made from a class, individual instances of classes with variables used to name them, can be manipulated in the program
- Has behaviors and attributes defined by the class used to create it
- They do not have an impact on one another (different objects)
- Can have 2 variables for one object
Changing one will change both variables
Constructors are used to initialize the attributes for an object
// I made an algorithm to calculate the 5 number summary that we use in AP Statistics. It uses the round() Math method (even though that technically makes it less accurate).
import java.util.Scanner; // Import the Scanner class
import java.util.ArrayList; // Import the ArrayList class
import java.util.Collections; // // Sort myData
class Main { // Defining the class
public static void main(String[] args) {
ArrayList<Integer> myData = new ArrayList<Integer>(); // Integer is a wrapper class
// Create an ArrayList object
Scanner firstData = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter a point of data");
myData.add (firstData.nextInt()); // Read user input
System.out.println("Data Inputted:" + myData);
String response = "y";
while (response.equals("y")) { // Use of the equals() String method
Scanner userResponse = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter more data? (y/n)");
response = userResponse.nextLine();
if (response.equals("y")){
Scanner Data = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter a point of data");
myData.add (Data.nextInt()); // Read user input
System.out.println("Data Inputted:" + myData);
}
}
Collections.sort(myData);
int last = myData.size();
System.out.println("Minimum: " + myData.get(0));
System.out.println("Q1: " + myData.get(Math.round((last-1)/4))); // Use the the round() Math method
System.out.println("Median: " + myData.get(Math.round((last-1)/2))); // Use the the round() Math method
System.out.println("Q3: " + myData.get(Math.round((last-1) *3/4))); // Use the the round() Math method
System.out.println("Maximum: " + myData.get(last-1));
}
}
Main.main(null);