Lab # 1 Introduction To Oop: Objective
Lab # 1 Introduction To Oop: Objective
LAB # 1
INTRODUCTION TO OOP
OBJECTIVE:
To understand OOP(Java Environment),Data Types and Mixed Arthematic
Expression.
THEORY:
Basic Terminologies:
Object:
It’s the instance of a class/ it’s the working entity of a class.
Class:
It is a template or blueprint about the capability of what an object can do.
Method:
The behaviors of a class. It tells what a method can do.
Instance:
Object and Instance both are same with small difference.
Object Class
Inheritance
Polymorphism
Abstraction
Encapsulation
3. In the New Project wizard, expand the Java category and select Java
Application as shown in the figure below. Then click Next.
4. In the Name and Location page of the wizard, do the following (as
shown in the figure below):
o In the Project Name field, type HelloWorldApp.
5. Click Finish.
The project is created and opened in the IDE. You should see the following
components:
The Projects window, which contains a tree view of the components of
the project, including source files, libraries that your code depends on,
and so on.
The Source Editor window with a file called HelloWorldApp open.
The Navigator window, which you can use to quickly navigate between
elements within the selected class.
The file should look something like the following code sample.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package helloworldapp;
/**
*
* @author <your name>
*/
public class HelloWorldApp {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
System.out.println("Hello World!");
}
the areas of the file that have errors, including errors in lines that are not
visible. You can mouse over an error mark to get a description of the error.
You can click a glyph in the right margin to jump to the line with the error.
You now know how to accomplish some of the most common programming
tasks in the IDE.
Java defines eight simple (or elemental) types of data: byte, short, int, long,
char, float, double, and boolean. These can be put in four groups:
Integers: This group includes byte, short, int, and long, which are for
whole valued signed numbers.
Long: It is a signed 64-bit type and is useful for those occasions where
an int type is not large enough to hold the desired value.
Integers
Floating-Point Types
Double: The
double variable can hold very large (or small) numbers. The maximum
and minimum values are 17 followed by 307 zeros.
Here is a short program that uses double variables to compute
addition.
Explicit Casting
Type Casting in Java is nothing but converting a primitive or interface or
class in into other type. There is a rule in Java Language that classes or
interface which shares the same type hierarchy only can be type casted. If
there is no relationship between then Java will throw ClassCastException.
Type casting are of two types they are
1. Implicit Casting (Widening)
2. Explicit Casting (Narrowing)
Example:
No Explicit casting required for the above mentioned sequence.
public class Implicit_Casting_Example
{
public static void main(String args[])
{
byte i = 50;
// No casting needed for below conversion
short j = i;
int k = j;
long l = k;
float m = l;
double n = m;
Example:
public class Explicit_Casting_Example
{
public static void main(String args[])
{
double d = 75.0;
// Explicit casting is needed for below conversion
float f = (float) d;
long l = (long) f;
int i = (int) l;
short s = (short) i;
byte b = (byte) s;
LAB TASK
Example:
Test Data
Input a value for inch: 1000.
Expected Output:
1000.0 inch is 25.4 meters.
2.Write a Java program to convert days into number of months and days.
Example:
Test Data
Input the number of days: 69.
Expected Output:
69 days are 2 months and 9 days.