Casting, specifically for Division

  • integer and integer resulting in integer
  • if you want it to become a decimal, make it into a double
  • type promotion (with an integer and a double value) makes it more complicated, resulting in the decimal
//example
public class Variables {
    public static void main (String[] args) {    

    }
}
System.out.println(10.0/15);
0.6666666666666666
public class Variables
{
   public static void main(String[] args)
   {
   }
}

int x=10;
int y=15;
System.out.println(1 / 3);
System.out.println(1.0 / 3);
System.out.println(1 / 3.0);
System.out.println((double) x / y);
//type promote; produces decimal answer
0
0.3333333333333333
0.3333333333333333
0.6666666666666666
//modulus
public class Variables
{
   public static void main(String[] args)
   {
   }}

System.out.println(15%10);
5

Casting, specifically for Truncating or Rounding

  • truncation vs. rounding
  • Rounding a number in the range [2.5, 3.5) returns the number 3
  • Truncating a number in the range [3.0, 4.0) returns the number 3
    • Where a square bracket '[' denotes inclusion in the range, and a parenthesis '(' denotes non-inclusion in the range -- ie. [3.0, 4.0) is the range of all numbers between 3 and 4, including 3 but not including 4.
  • truncation; chops off decimal portion
//truncation
public class truncation
{
   public static void main(String[] args)
{}}
//example
int a, b, c;
double d, e;

//declare variables
a = 5;
b = -13;
d = 2.8;

c = a + (int)d; //typecasting
//truncation, cut off decimal
//d became 2 instead of 2.8, so 2 + 5 = 7

System.out.println("c is: " + c);
c is: 7
//rounding
public class rounding
{
   public static void main(String[] args)
{}}

double number = 2.8;
System.out.println( "before rounding: " + number ); 

number = Math.round(number);
System.out.println( "after rounding: " + number ); 

double number = 2.4;
System.out.println( "before rounding: " + number ); 

number = Math.round(number);
System.out.println( "after rounding: " + number );
before rounding: 2.8
after rounding: 3.0
before rounding: 2.4
after rounding: 2.0

Concatenation

  • appendage of strings
//ex
String firstName = "John ";
String lastName = "Doe";
System.out.println(firstName.concat(lastName));
John Doe
class concatenation
{
    public static void main(String arg[])
    {
        }
}

String str1 = "hello", str2 = " to you", str3 = " too";
str1 = str1 + str2 + str3; // LINE A
System.out.println(str1);
//line a is '+' operator

str2 = str2.concat(str3);
System.out.println(str2); //line B
//concat() method

boolean folklore = true;
boolean evermore = false;
System.out.println("Folklore is a great album." + "\nThis statement is " + folklore); // LINE C
System.out.println("Folklore is a bad album." + "\nThis statement is " + evermore);
//concatenation of a boolean to a string
hello to you too
 to you too
Folklore is a great album.
This statement is true
Folklore is a bad album.
This statement is false

Math Classes (Random Usage)

  • methods to perform math tasks on numbers
  • random() returns a random number between 0 (inclusive) and 1
  • public static double random^
import java.lang.Math;
 
//java.lang.Math.random()
class random {
 
    // driver code
    public static void main(String args[])
    {
    }
}
double rand = Math.random();
System.out.println("Random Number:" + rand);
Random Number:0.7941694146696837

Type Casting in Java

  • when you assign a value of one primitive data type to another type.

  • two types of casting in Java:

           - Widening Casting (automatically) - converting a smaller type to a larger type size
                 byte -> short -> char -> int -> long -> float -> double
    
           - Narrowing Casting (manually) - converting a larger type to a smaller size type
                 double -> float -> long -> int -> char -> short -> byte
  • Why wrap int, double, etc?
  • way to use primitive data types as objects

q1

  • primitive type and corresponding wrapper class
  • Wrapper classes are used to convert any data type into an object. The primitive data types are not objects; they do not belong to any class; they are defined in the language itself. Sometimes, it is required to convert data types into objects in Java language.

HOMEWORK

//HW
//2006 FRQ

//2a
public double purchasePrice() {
    return getListPrice() * (1+taxRate);
}
//3a
public int compareCustomer(Customer other)
  {
    int compare =  getName().compareTo(other.getName());
    if (compare == 0) {
      return getID() - other.getID();
    }
    else {
      return compare;
    }
  }