Programming Home Work 2
Programming Home Work 2
1. Introducti on
In this practical you learn basics of java such as Variables, Data Types and other basics activities.
Data types are divided into two groups:
Primitive data types - includes byte, short, int, long, float, double, boolean and char
Non-primitive data types - such as String, Arrays and Classes
PRIMITIVE DATA TYPES
A primitive data type specifies the size and type of variable values, and it has no additional methods.
There are eight primitive data types in Java:
Data Size Description
Type
byte 1 byte Stores whole numbers from -128 to 127
double 8 bytes Stores fractional numbers. Sufficient for storing 15 decimal digits
Non-primitive
NON-PRIMITIVE data types
DATA are called reference types because they refer to objects.
TYPES
The main difference between primitive and non-primitive data types are:
Primitive types are predefined (already defined) in Java. Non-primitive types are created by the
programmer and is not defined by Java (except for String).
Non-primitive types can be used to call methods to perform certain operations, while primitive types
cannot.
A primitive type has always a value, while non-primitive types can be null.
A primitive type starts with a lowercase letter, while non-primitive types start with an uppercase letter.
The size of a primitive type depends on the data type, while non-primitive types have all the same size.
Examples of non-primitive types are Strings, Arrays, Classes, Interface, etc.
Primitive
NUMBERSnumber types are divided into two groups:
(PRIMITIVES)
Integer types stores whole numbers, positive or negative (such as 123 or -456), without decimals. Valid
types are byte, short, int and long. Which type you should use, depends on the numeric value.
Floating point types represents numbers with a fractional part, containing one or more decimals.
There are two types: float and double.
Even though there are many numeric types in Java, the most used for numbers are int (for whole numbers)
and double (for floating point numbers).
Page 1 of 4
float myFloatNum = 5.99f; // Floating point number
Page 2 of 4
INTEGER TYPES
Byte The byte data type can store whole numbers from -128 to 127. This can be used instead of int or other integer types
to save memory when you are certain that the value will be within -128 and 127:
Short
The short data type can store whole numbers from -32768 to 32767:
Int The int data type can store whole numbers from -2147483648 to 2147483647. In general, and in our tutorial,
the int data type is the preferred data type when we create variables with a numeric value.
Long The long data type can store whole numbers from -9223372036854775808 to 9223372036854775807. This is used
when int is not large enough to store the value. Note that you should end the value with an "L":
FLOATING POINT TYPES
You should use a floating-point type whenever you need a number with a decimal, such as 9.99 or 3.14515.
FLOAT
The float data type can store fractional numbers from 3.4e−038 to 3.4e+038. Note that you should end the value
with
an "f":
DOUBLE
The double data type can store fractional numbers from 1.7e−308 to 1.7e+308. Note that you should end the value
with a "d":
Which one to use Float or Double?
The precision of a floating-point value indicates how many digits the value can have after the decimal
point. The precision of float is only six or seven decimal digits, while double variables have a precision of
about 15 digits.
Therefore, it is safer to use double for most calculations.
SCIENTIFIC NUMBERS
A floating point number can also be a scientific number with an "e" to indicate the power of 10:
float f1 = 35e3f; double d1 =
12E4d; System.out.println(f1);
System.out.println(d1); char
myGrade = 'B';
System.out.println(myGrade);
BOOLEANS A boolean data type is declared with the boolean boolean isJavaFun = true; boolean
keyword and can only take the values true or false: isFishTasty = false;
System.out.println(isJavaFun);
Boolean values are mostly used for conditional testing, System.out.println(isFishTasty);
CHARACTE The char data type is used to store a single character. char myGrade = 'B';
RS The character must be surrounded by single quotes, System.out.println(myGrade);
like
'A' or 'c':
Alternatively, you can use ASCII values to display certain characters:
Page 4 of 4
JAVA TYPE CASTING
Type casting is when you assign a value of one primitive data type to another
type. In Java, there are two types of casting:
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
Page 5 of 4
Java Simple Programs
//Java Program to check Even or Odd Output 1:
number import java.util.Scanner;
class CheckEvenOdd{
public static void main(String args[]) { int Enter an Integer number:
num; 78
Entered number is even
System.out.println("Enter an Integer number:");
Output 2:
//The input provided by user is stored in num
Scanner input = new Scanner(System.in);
num = input.nextInt(); Enter an Integer number:
/* If number is divisible by 2 then it's an even 77
number * else odd number*/ Entered number is odd
if ( num % 2 == 0 )
System.out.println("Entered number is even");
else
System.out.println("Entered number is odd");
}
}
//Program to check whether the input year is leap or not Here we will write a java program to
import java.util.Scanner; check whether the input year is a leap
public class Demo { year or not. Before we see the program,
public static void main(String[] args) { int lets see how to determine whether a
year; year is a leap year mathematically:
Scanner scan = new To determine whether a year is a leap
Scanner(System.in); year, follow these steps:
System.out.println("Enter any Year:"); 1. If the year is evenly divisible by 4, go to step
year = scan.nextInt(); 2. Otherwise, go to step 5.
scan.close(); 2. If the year is evenly divisible by 100, go
boolean isLeap = false; to step 3. Otherwise, go to step 4.
if(year % 4 == 0) { 3. If the year is evenly divisible by 400, go
if( year % 100 == 0) { to step 4. Otherwise, go to step 5.
if ( year % 400 == 0) 4. The year is a leap year (it has 366 days).
isLeap = true; 5. The year is not a leap year (it has 365 days).
else
isLeap = false; Output:
}
else Enter any Year:
isLeap = true; 2001
}else { 2001 is not a Leap Year.
isLeap = false;
}
if(isLeap==true)
System.out.println(year + " is a Leap Year.");
else
System.out.println(year + " is not a Leap Year.");
}
}
//Program to calculate power of a number using for loop In this program we are calculating the
public class JavaExample { power of a given number using for loop.
public static void main(String[] args) {
//Here number is the base and p is the exponent Here number is the base and p is the power
int number = 2, p = 5; (exponent). So we are calculating the result
long result = 1; of number^p.Program to calculate power of
//Copying the exponent value to the loop counter a number using pow() function In the above
int i = p; two programs, we have seen how to
for (;i != 0; --i) calculate power of a number using loops. In
{ the following program, we are using pow()
result *= number;
Page 6 of 4
} function to calculate power.
//Displaying the output public class JavaExample {
System.out.println(number+"^"+p+" = "+result);
} public static void main(String[] args)
} { int number = 10, p = 3;
double result =
Math.pow(number, p);
System.out.println(number+"^"+p
+" =
"+result);
}
}
Page 7 of 4