If we divide or perform operations on integers then the fraction is discarded.
int x1 = 3 / 2 ;
will produce 1.5 . If we try to assign a wider to narrow conversion then we will receive a compiler error.
int x1 = 3.0 + 1.5 ;
We can assign a narrow to wider conversion without a compiler error.
double d1 = 100 ;
Why does Java do this ? We don't want a variable on the left having a "wrong" value .
File: "conv2.java"
public class conv2
{
public static void main( String args[] )
{
int x1 = 80023 ;
char ch ;
ch = (char) x1 ;
System.out.println((int) ch ) ;
}
}
Output:
C:\Java\FinalReview>java conv2
14487
The above example shows how a narrowing cast may change the value in the variable on the left hand side.
If we have an expression that is assigned to a wider type and in that expression we have a single value that is of the same wider type then other values in that expression get promoted to that type.
File: "conv1.java"
public class conv1
{
public static void main( String args[] )
{
int x1 = 3 / 2 ;
System.out.println( x1 ) ;
//Compiler error wide to narrow conversion
//x1 = 3 + 1.5 ;
//System.out.println( x1 ) ;
double f1 = 5 / 2 ;
System.out.println( "5 / 2 f1: " + f1 ) ;
f1 = 5.0 / 2 ;
System.out.println( "5.0 / 2 f1: " + f1 ) ;
f1 = 5.0 / 2.0 ;
System.out.println( "5.0 / 2.0 f1: " + f1 ) ;
f1 = 3 / ((double)2) ;
System.out.println( "f1 / ((double)3) 2: " + f1 ) ;
f1 = (double) ( 5 / 2 ) ;
System.out.println( "(double) ( 5 / 2 ): " + f1 ) ;
}
}
Output:
C:\Java\FinalReview>java conv1
1
5 / 2 f1: 2.0
5.0 / 2 f1: 2.5
5.0 / 2.0 f1: 2.5
f1 / ((double)3) 2: 1.5
(double) ( 5 / 2 ): 2.0
FIle: "mod1.java"
public class mod1
{
public static void main( String args[] )
{
int x1 = 15 ;
System.out.println( x1++ ) ;
System.out.println( ++x1 ) ;
System.out.println( 15 % 5 ) ;
System.out.println( 15 % 6 ) ;
}
}
Output:
C:\Java\FinalReview>java mod1
15
17
0
3
Boolean operators
File: "op.1.java"
public class op1
{
public static void main( String args[] )
{
int x1 = 15 ;
int y1 = 15 ;
boolean b1 = false ;
boolean b2 = true ;
boolean b3 = false ;
System.out.println( x1 == 15 ) ;
System.out.println( ( x1 == 14 ) ||
b2 ) ;
System.out.println( ! b1 ) ;
System.out.println( (!b3 && b1 ) || ( b1 || b2 ) ) ;
}
}
Output:
C:\Java\FinalReview>java op1
true
true
true
true
File: "loop1.java"
public class loop1
{
public static void main( String args[] )
{
for( int i1=0 ; i1< 4 ; i1++ )
{
for( int j1=i1 ; j1< 2; j1++ )
{
System.out.println( i1 + j1 ) ;
}
}
}
}
Output:
0
1
2
File: "loop2.java"
public class loop2
{
public static void main( String args[] )
{
for( int i1=0 ; i1< 4 ; i1++ )
{
int j1 = i1 + 1 ;
for( ; j1 < 3 ; j1++ )
{
System.out.println( j1 ) ;
j1++ ;
}
}
}
}
Output:
1
2
File: "loop3.java"
public class loop3
{
public static void main( String args[] )
{
int sum = 0 ;
for( int i1=0 ; i1< 4 ; i1++ )
{
sum += i1 ;
}
for( int j1=0 ; j1<2 ; j1++ )
{
sum += j1 ;
}
System.out.println( sum ) ;
}
}
Output:
7
File: "loop4.java"
public class loop4
{
public static void main( String args[] )
{
int arr1[] = { 1, 2, 3 , 4 } ;
for( int i1=3 ; i1 > 1 ; i1-- )
{
arr1[i1-1] = arr1[i1] ;
}
for( int j1=1 ; j1<3 ; j1++ )
{
arr1[j1+1] = arr1[j1] ;
}
System.out.println( Arrays.toString(arr1) ) ;
}
}
Output:
[1,4,4,4]
Study the container classes Vector and ArrayList and the operations/methods these classes have.
File: "vect1.java"
import java.util.* ;
public class vector1
{
public static void main( String args[] )
{
Vector<Integer> v1 = new Vector<Integer>() ;
v1.add(1);
v1.add(2);
v1.add(3);
v1.add(4);
System.out.println( v1.size() ) ;
System.out.println( v1 ) ;
int x1 = v1.remove(2) ;
System.out.println(v1) ;
System.out.println( v1.add( x1 ) ) ;
System.out.println(v1) ;
}
}
Output:
import java.util.* ;
public class vector1
{
public static void main( String args[] )
{
Vector<Integer> v1 = new Vector<Integer>() ;
v1.add(1);
v1.add(2);
v1.add(3);
v1.add(4);
System.out.println( v1.size() ) ;
System.out.println( v1 ) ;
int x1 = v1.remove(2) ;
System.out.println(v1) ;
System.out.println( v1.add( x1 ) ) ;
System.out.println(v1) ;
}
}
Classes
Create a class called "Person". It should have 2 attributes an "id" and "name" . It should have a constructor to set these 2 variables. It should have a "toString()" method that prints out the 2 variables. It should have accessor and mutator methods for the 2 variables.
Create a class called "employee" that inherits from "Person" and should have a constructor that takes 3 arguments. The 3 arguments are "id", "name" and "title". The "title" is a String variable that "employee" defines and the other 2 it inherits from "Person".
It should call the base class constructor to set the 2 variables "id" and "name" and then
sets the "title" variable.
It should also have a "toString()" method that prints out the 3 variables. Your main should
create objects of Person and employee and use "System.out.println" to print out the objects.
File: "employee.java"
class Person
{
int id ;
String name ;
Person( int idP , String nameP )
{
id = idP ;
name = nameP ;
}
//accessor method
int getId ()
{
return id ;
}
//setter method
void setId( int idP )
{
id = idP ;
}
public String toString()
{
return( "Id:" + id + " Name: " + name ) ;
}
}
class employee extends Person
{
String title ;
employee( int idP, String nameP , String titleP )
{
super( idP , nameP ) ;
title = titleP ;
}
public String toString()
{
return( "Id:" + id + " Name: " + name +
" Title:" + title ) ;
}
public static void main( String args[] )
{
Person obj1 = new Person( 1, "Ajay" ) ;
System.out.println( obj1 ) ;
employee obj2 = new employee( 1, "Ajay" , "Instructor" ) ;
System.out.println( obj2 ) ;
}
}
Output:
[student@milhouse ajay]$ java employee
Id:1 Name: Ajay
Id:1 Name: Ajay Title:Instructor