x = 5
print("My number is", x)
What is:
int x = 5;
System.out.println("My number is " + x);
BONUSSSS
more freee pointsss
What are the two types of variables in Java?
What is Primitives and objects/references
how do we form a "contract" with an interface
implements
What two methods must be overridden to use a Hash-based data structure?
FREE POINTSSSS
What is a constructor?
What is:
a method that creates an object and sets the fields of said object. It then returns a reference to that object.
In what two situations is toString automatically called?
What is
printing
and String Concatenation
how many interfaces and abstract classes can in inherent from
interfaces: infinite
abstract class: 1
What uses natural order and what function does it call.
What uses custom order and what function does it call.
natural: comparable interface with compareTo
custom: comparator class with compare
a = [0, 1, 2, 3, 4, 5]
for x in a:
print(x)
What is:
int[] a = {0, 1, 2, 3, 4, 5};
for(int x: a){
System.out.println(x);
}
This is used to reference the current reference of the class.
What is "this"
what is Object
what is super(param1, ...., paramN)
Time complexity for contains for these classes:
TreeSet, HashSet, ArrayList
TreeSet: O(logn)
HashSet: O(1)
ArrayList: O(n)
x = 100
total = 0
for i in range(x):
if i % 7 == 0 or i % 3 == 0 and i % 2 == 0:
total += i
print(total)
int x = 100;
int total = 0;
for (tint i = 0; i < x; i++){
if (i % 7 == 0 || i % 3 == 0 && i % 2 == 0){
total += i;
}
}
System.out.println(total);
protected static final double VAR = 5.23;
this variable can be accessed by the class and subclasses. Likewise, its memory is shared between all instances of the class, and finally, it's constant such that it can be changed.
What are the 4 pillars of OOP: EXPLAIN THEM
(Hint: I PEA)
Inheritance
Polymorphism
Encapsulation
Abstraction
write an Abstract class for a Person:
3 fields: name, age, alive (default = true)
2 methods
abstract speak and getName
SPRINT:
Assuming we have a Person class with the method getAge write our compareTo method to sort the person by descending age.
@Override
public int compareTo(Person o){
return age - o.age;
}
def print_range(start, end, step):
for i in range(start, end, step):
print(i)
def main():
step1, step2= int(input()), int(input())
print_range(100, 0, step1)
print_range(0, 100, step2)
# hint use a boolean set to start < end and use that in your comparison in the for loop
What is import java.util.Scanner;
private static void printRange(int start, int end, int step){
boolean greater = start < end;
for (int i = start; i < end == greater; i += step){
System.out.println(i);
}
}
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
int step1 = scan.nextInt();
int step2 scan.nextInt();
printRange(100, 0, step1);
printRange(0, 100, step2);
}
SPRINT!!!!!
write a Java class about a book.
3 feilds: name, price, author
3 methods: getName, getPrice, equals
What is:
public class Book{
private String name;
private String author;
private double price;
public Book(String name, String author, double price){
this.name = name; this.author = author; this.price = price;
}
public String getName(){ return this.name; }
public double getPrice(){ return this.price; }
@Override
public boolean equals(Object o){
boolean result = false;
if (o instanceof Book book){
result = this.name.equals(book.name) && this.author.equals(book.author);
}
return result;
}
}
SPRINT!!!!
Given this outline draw the UML:
an interface Animal with the following methods:
speak, travel with integer, eat which takes in food
A food class with a name, and amount
An abstract class Dog
with a fur color and an abstract method bark that returns a string.
Finally two classes:
a fish which implements Animal
and Fido which implements Animal and extends Dog.
yes
write a generic stack interface
int getSize();
boolean isEmpty();
void push(E value);
E top();
E remove();
}SPRINT!!!!
Assuming we have a Person class with a getName and getAge function, write a Comparator Class that orders a person by accessing name and descending age.
public class PersonComparator implements Comparator<Person>{
public PersonComparator(){}
@Override
public int compare(Person o1, Person o2){
int result = o1.getName().compareTo(o2.getName())
return result != 0 ? result : o2.getAge() - o1.getAge();
}
}