CS F213 Objected Oriented Programming Labsheet 1
Spring 2024
Write your first JAVA program.
Compilation:
Compile your code on a terminal:
In this case:
Execution:
Execute your code through terminal by writing
In this case:
Output:
After your code is compiled, you will find a file with the same name as the class name declared in the file but with the extension .class. A Java class file is a file containing Java bytecode and having .class extension that can be executed by JVM (Java Virtual Machine). A Java class file is created by a Java compiler from .java files as a result of successful compilation. The compilation will result in as many .class files as there are classes in the file you just compiled. A good practice, but not a requirement, is to restrict to one class per java file.
Java Class and Structure
Class:
The Class is the basic unit of Object Oriented Programming. The Class forms the basis for object oriented programming in Java.
General Syntax of a Class:
Two components of Java Class:
Attributes - Variables
Methods - Functions
Example:
To instantiate an object of Square:
To access variables of an object
To access methods of an object
Syntax Guidelines
Every line of code that runs in Java must be inside a
class
A good practice is to use CamelCase with the first character capitalised for class names. Class names are usually nouns
A good practice is to use camelCase with the first character in lower case for methods. Method names are usually verbs
The name of the Java file need not match the class name but it is good practice to do so (see Note below).
The
main()
method is required and you will see it in every Java program. Any code inside themain()
method will be executed.
Note: If we are declaring a public class, then it must be in a file of the same name.
Java Identifiers
All Java variables must be identified with unique names called identifiers.
Declaring (Creating) Variables:
Types of Java Literals:
String
- Stores textint
- Stores integers (whole numbers)float
- Stores floating point numberschar
- Stores single charactersboolean
- Stores true or false
Variable Name Guidelines
Variable names are usually in all lowercase but meaningful
Names can contain letters, digits, underscores, and dollar signs
Names must begin with a letter (_ and $ are allowed as the first character but discouraged)
Names should start with a lowercase letter and it cannot contain whitespace
Names are case sensitive
Reserved words (keywords) cannot be used as names
Java Conditional Statements
if
else
else if
switch
Shortcut for if-else
Java Loops
while loop
do-while loop
for loop
for-each loop (can be used for iterating)
Java Data Types
byte
8-bit
Byte-length integer
short
16-bit
Short integer
int
32-bit
Integer
long
64-bit
Long integer
float
32-bit
Single-precision floating point
double
64-bit
Double-precision floating point
char
16-bit
Unicode character A single character
boolean
true or false
A boolean value (true or false)
Array
Declaring an array
Example
Declaring a 2D array
Example
Taking input from user
The Scanner class is used to get user input, and it is found in the java.util package.
nextBoolean()
Reads a boolean value from the user
nextByte()
Reads a byte value from the user
nextDouble()
Reads a double value from the user
nextFloat()
Reads a float value from the user
nextInt()
Reads a int value from the user
nextLine()
Reads a String value from the user
nextLong()
Reads a long value from the user
nextShort()
Reads a short value from the user
Exercise Problems:
Exercise 1
What happens when we declare multiple classes in the same file and then use javac to compile the file and the file name is any one of the declared classes?
Exercise 2
Write a Java program to compute the final amount a person has to repay for a loan of 10 years with interest rate of 5% per annum. The principal amount is to be taken as input from user and display the final amount.
Use double
datatype for principal and final amount.
Formula:
FinalAmount = PrincipalAmount(1 + (TimePeriodInYears * InterestRate / 100))
Input
A double representing the principal amount
Output
A double representing the final amount
Sample Test Input
Sample Test Output
Exercise 3
Write a Java program to print a menu to the user asking to choose between by entering the corresponding option number:
If 1
(circle) is chosen, take the input of radius (int) and print the area of the circle (take pi = 3.14), otherwise take the input of length (int) and breadth (int) of rectangle and print the area.
Do this exercise with both if-else and switch statements.
Sample Test Input 1
Sample Test Output 1
Sample Test Input 2
Sample Test Output 2
Exercise 4
Write a Java program to declare a 2D array of size 4 X 3, then take the input from user such that the array contains following elements:
After that, using for-each loop find the sum of the whole array and print it.
***
Last updated