Primitive Types Lesson Hacks
Hacks from the presentation
// 6
int i = 5; int p =27;
for(int l = 23; l <p; l++){
i*=(l-22);
}
System.out.print(i);
// 5
int i = 100;
double d = 4.55; double d2 = 3.75;
int j = (int) (d*100 + d2);
System.out.print(j);
//3
int val = 205;
for(int i=0; i<5; i++) {
val/=2;
}
System.out.print(val);
//4
int i = 3;
for(int j = 5;j > 0; j--){
i+=i;
}
System.out.print(i);
- 1: 2
- 2: A and D
- 3: 6
- 4: 96
- 5: 458
- 6: 120
// 2A
public double purchasePrice(double x){
x *= 1.1;
return x;
}
purchasePrice(6.50);
// 3A
public class Customer {
private String name;
private int id;
public Customer(String name, int idNum){
this.name = name;
this.id = idNum;
// idNum = this.idNum;
};
public String getName(){
return name;
};
public int getID(){
return id;
};
public void compareCustomer(Customer x){
System.out.println(id - x.id);
// return (id - x.id);
};
}
Customer c1 = new Customer("Smith", 1001);
Customer c2 = new Customer("Anderson", 1002);
Customer c3 = new Customer("Smith", 1003);
c1.compareCustomer(c1);
c1.compareCustomer(c2);
c1.compareCustomer(c3);
Vocab used in this unit
- this keyword - Used in the Customer class, the this keyword has the object refer to its own attributes to modify or get them.
- Constructor - Creates an object using given parameters, and assigns them to the object's attributes. It has no return because it is only used to create an object and not to run a method or a calculation.
-
Comparing numbers / objects - Seen in
Customer.compareCustomer
, this method uses the this keyword to get and compare the customer's ID and another customer's ID through subtraction.