Unit 1 Homework
Unit 1 FRQ and Final Grade Calculator
- Final Grade Calculator
- Java Casting
- Casting Division
- Concatenation
- Late Binding of Object
- Referencing Superclass Objects
- Main Method
- Tester Method
- Overriding a Method
- Overloading a Method
- Inheritance/Extends
- Abstract Class and Abstract Method
- Static and Class Variables
- Static Method and Class Method
- Subclass Constructor and Super Keyword
Notes: Byte: minimum value of -128 and maximum value of 127 (inclusive). It is useful for saving memory in large arrays Short: Minimum value of -32,768 and maximum value of 32,767. Same purpose as Byte Int: Any integer or whole number Long: Greater range than int Float: floating point numbers that tend to have decimals Double: not good for precise data Boolean: logic that evaluates whether a condition is true or false Char
Part A
public DownloadInfo getDownloadInfo(String title) {
for (DownloadInfo info : downloadList){
if (info.getTitle().equals(title)){
return info;
}
}
return null; // method inside the class
}
Part B
public void updateDownloads(List<String> titles) {
for (String title : titles) {
DownloadInfo foundInfo = getDownloadInfo(title); // calls method
if (foundInfo == null){
downloadList.add(new DownloadInfo(title)); // makes new object
}
else {
foundInfo.incrementTimesDownloaded();
}
}
}
// java style to import library
import java.util.Scanner;
// class must alway have 1st letter as uppercase, CamelCase is Java Class convention
public class ScanPrimitives {
public static void main(String[] args) {
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Is the final in a seperate category? True to False");
boolean tof = myObj.nextBoolean(); // Read user input
System.out.println("Final in a seperate category is " + tof); // Output user input
if (tof) {
System.out.println("What is your current grade rn?");
double grade = myObj.nextDouble();
System.out.println(grade);
System.out.println("What is % of grade that is final?");
double percent = myObj.nextDouble();
System.out.println(percent + "%");
System.out.println("What is your desired grade?");
double desired = myObj.nextDouble();
System.out.println(desired);
double output = (desired - (grade * ((100-percent)/100)))/(percent/100);
System.out.println("You need a " + output + " on your final");
}
else {
System.out.println("What is your current grade rn?");
double grade = myObj.nextDouble();
System.out.println(grade);
System.out.println("What is the % of grade that is test category?");
double percent = myObj.nextDouble();
System.out.println(percent + "%");
System.out.println("What is current % in tests category?");
double currentpercent = myObj.nextDouble();
System.out.println(currentpercent + "%");
System.out.println("What is the current amount of points in the test category?");
int currentpts = myObj.nextInt();
System.out.println(currentpts);
System.out.println("How many pts is the final?");
int pts = myObj.nextInt();
System.out.println(pts);
System.out.println("What is your desired grade?");
double desired = myObj.nextDouble();
System.out.println(desired);
// double output = (desired - ((grade * currentpercent)*((100 - percent) / 100)))/((currentpts + pts)/(percent / 100))- currentpts;
double output = ((desired - grade+currentpercent)*(currentpts+pts)-currentpts*currentpercent)/100;
System.out.println("You need " + output + " pts on your final");
}
}
}
ScanPrimitives.main(null);
Type casting is when you assign a value of one primitive data type to another type.
public class Main {
public static void main(String[] args) {
int myInt = 9;
double myDouble = myInt; // Automatic casting: int to double
System.out.println(myInt); // Outputs 9
System.out.println(myDouble); // Outputs 9.0
}
}
public class Main {
public static void main(String[] args) {
double myDouble = 9.78d;
int myInt = (int) myDouble; // Manual casting: double to int
System.out.println(myDouble); // Outputs 9.78
System.out.println(myInt); // Outputs 9
}
}
import java.lang.Math;
public class Program {
public static void main(String[] args) {
double test1 = 1.234;
double test2 = -1.567;
// ... Math.floor will always go towards negative infinity.
System.out.println(Math.floor(test1));
System.out.println(Math.floor(test2));
// ... Math.round may go towards negative or positive infinity.
System.out.println(Math.round(test1));
System.out.println(Math.round(test2));
// ... Casting to int will remove the fractional part.
// This truncates the number.
System.out.println((int) test1);
System.out.println((int) test2);
}
}
double d=99.99999999; // Rounds 99 to 100
System.out.println( Math.round(d));
If we divide two integers, even if we are setting them to a double, we will always be returned an integer.
int currResidents = 50;
int floorTotal = 56;
double average = floorTotal / currResidents;
// 'average' will return the value '1'
Concatenation
(+) or +=
Joining of data
String concatenation
Primitive concatenation with strings (automatically convert primitives to String objects)
String + object concatenation (will call object's toString() method)
Animal a = new Chicken();
Animal b = new Goat();
class MyClass {
// main method
public static void main (String[] args) {
MyClass obj = new MyClass();
}
}
MyClass.main(null);
Overriding a Method
The ability of a subclass to override a method allows a class to inherit from a superclass whose behavior is "close enough" and then to modify behavior as needed. The overriding method has the same name, number and type of parameters, and return type as the method that it overrides.
Overloading a Method
Overloading happens when you have two methods with the same name but different signatures (or arguments). In a class we can implement two or more methods with the same name. Overloaded methods are differentiated based on the number and type of parameter passed as arguments to the methods.
public class PainterPlus extends Painter {
Abstract Class and Abstract Method
Abstract Class in Java does the process of hiding the intricate code implementation details from the user and just provides the user with the necessary information.
Abstract method: can only be used in an abstract class, and it does not have a body. The body is provided by the subclass (inherited from).
// Abstract class
abstract class Animal {
// Abstract method (does not have a body)
public abstract void animalSound();
// Regular method
public void sleep() {
System.out.println("Zzz");
}
}
// Subclass (inherit from Animal)
class Pig extends Animal {
public void animalSound() {
// The body of animalSound() is provided here
System.out.println("The pig says: wee wee");
}
}
class Main {
public static void main(String[] args) {
Pig myPig = new Pig(); // Create a Pig object
myPig.animalSound();
myPig.sleep();
}
}
Static and Class Variables
All variables which are assigned a value in the class declaration are class variables.
Static variables are allocated memory ones when the object for the class is created for the first time. Static variables are created outside of methods but inside a class Static variables can be accessed through a class but not directly with a instance. Static variables behavior doesn’t change for every object.
# assigned in class declaration, are class variables
# Class for Computer Science Student
class CSStudent:
stream = 'cse' # Class Variable
def __init__(self,name,roll):
self.name = name # Instance Variable
self.roll = roll # Instance Variable
# Objects of CSStudent class
a = CSStudent('Geek', 1)
b = CSStudent('Nerd', 2)
print(a.stream) # prints "cse"
print(b.stream) # prints "cse"
print(a.name) # prints "Geek"
print(b.name) # prints "Nerd"
print(a.roll) # prints "1"
print(b.roll) # prints "2"
# Class variables can be accessed using class
# name also
print(CSStudent.stream) # prints "cse"
# Now if we change the stream for just a it won't be changed for b
a.stream = 'ece'
print(a.stream) # prints 'ece'
print(b.stream) # prints 'cse'
# To change the stream for all instances of the class we can change it
# directly from the class
CSStudent.stream = 'mech'
print(a.stream) # prints 'ece'
print(b.stream) # prints 'mech'
class MyClass {
// static method
static String coolMethod (String a) {
return a + " cool";
}
// static property
static int staticProp = 10;
public static void main(String[] args) {
// no object needed for any of this
System.out.println(MyClass.coolMethod("test"));
System.out.println(MyClass.staticProp);
}
}
MyClass.main(null);
Subclass Constructor and Super Keyword
A subclass inherits all the members (fields, methods, and nested classes) from its superclass. Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass.
A subclass needs a constructor if the superclass does not have a default constructor (or has one that is not accessible to the subclass). If the subclass has no constructor at all, the compiler will automatically create a public constructor that simply calls through to the default constructor of the superclass.
The super keyword refers to superclass (parent) objects. It is used to call superclass methods, and to access the superclass constructor. The most common use of the super keyword is to eliminate the confusion between superclasses and subclasses that have methods with the same name.
// using super to call the superclass of Dog (subclass)
class Animal { // Superclass (parent)
public void animalSound() {
System.out.println("The animal makes a sound");
}
}
class Dog extends Animal { // Subclass (child)
public void animalSound() {
super.animalSound(); // Call the superclass method
System.out.println("The dog says: bow wow");
}
}
public class Main {
public static void main(String args[]) {
Animal myDog = new Dog(); // Create a Dog object
myDog.animalSound(); // Call the method on the Dog object
}
}