You are on page 1of 25

19CSE204 Object oriented Programming

19CSE204 Object Oriented Programming


Lab Notes

From C to Java

Nalinadevi Kadiresan
CSE Dept.
Amrita School of Engg.
19CSE204 Object oriented Programming 2

About Java
• A programming language.
– As defined by Gosling, Joy, and Steele in the
Java Language Specification
• A platform
– A virtual machine (JVM) definition.
– Runtime environments in diverse hardware.
• A class library
– Standard APIs for GUI, data storage,
processing, I/O, and networking.

June 2021 Nalinadevi Kadiresan


19CSE204 Object oriented Programming

Java notes for C programmers


• Everything is an object.
– Every object inherits from java.lang.Object

• No code outside of class definition!


– No global variables.

• All classes are defined in .java files


– one top level public class per file

• There are two types of functions: static and non-


static
– main() method is a static function
3
June 2021
19CSE204 Object oriented Programming 4

Simple Java Program with only


main method

June 2021 Nalinadevi Kadiresan


19CSE204 Object oriented Programming 5

Hello world! program in C and Java


public class HelloWorld
#include <stdio.h> {
int main() { public static void main(String args[])
printf("Hello, World!"); {
return 0; System.out.println("Hello World");
}
}
}

• Save this file as HelloWorld.java


• To compile: javac HelloWorld.java
• To execute: java HelloWorld
• Output: Hello world

June 2021 Nalinadevi Kadiresan


19CSE204 Object oriented Programming 6

Compiling and Running

javac HelloWorld.java
HelloWorld.java
compile

source code
HelloWorld.class

bytecode
java HelloWorld
run
June 2021 Nalinadevi Kadiresan
19CSE204 Object oriented Programming 7

Common Syntax in C and Java


• Data types
• Operators
• Control statements
• Looping statements

June 2021 Nalinadevi Kadiresan


19CSE204 Object oriented Programming 8

Datatypes Common in C and Java

June 2021 Nalinadevi Kadiresan


19CSE204 Object oriented Programming 9

Additional Datatypes in Java

• Primitive : Boolean
• Non-primitive : String, classes, interfaces

• Java is a statically-typed programming language.


– It means, all variables must be declared before its use
– Example: int a; float b; String c; boolean c; char d; byte e;

June 2021 Nalinadevi Kadiresan


19CSE204 Object oriented Programming 10

Datatypes and its range - Java

June 2021 Nalinadevi Kadiresan


19CSE204 Object oriented Programming 11

Common Syntax in C and Java


• Data types
• Operators
• Control statements
• Looping statements

June 2021 Nalinadevi Kadiresan


19CSE204 Object oriented Programming 12

Operators common to C and Java

June 2021 Nalinadevi Kadiresan


19CSE204 Object oriented Programming 13

Operators common to C and Java


Increment and Decrement operators

Conditional operators

June 2021 Nalinadevi Kadiresan


19CSE204 Object oriented Programming 14

Operators common to C and Java

June 2021 Nalinadevi Kadiresan


19CSE204 Object oriented Programming 15

June 2021 Nalinadevi Kadiresan


19CSE204 Object oriented Programming 16

Common Syntax in C and Java


• Data types
• Operators
• Control statements
• Looping statements

June 2021 Nalinadevi Kadiresan


19CSE204 Object oriented Programming 17

Control statements in C and Java


• The Java programming language has two decision-making
statements: if-else and switch.
• Syntax of if statement:

June 2021 Nalinadevi Kadiresan


19CSE204 Object oriented Programming 18

Control statements in C and Java


• Syntax of switch statement:

June 2021 Nalinadevi Kadiresan


19CSE204 Object oriented Programming 19

Common Syntax in C and Java


• Data types
• Operators
• Control statements
• Looping statements

June 2021 Nalinadevi Kadiresan


19CSE204 Object oriented Programming 20

Loops in C and Java

June 2021 Nalinadevi Kadiresan


19CSE204 Object oriented Programming 21

Problem solving in C vs Java

June 2021 Nalinadevi Kadiresan


19CSE204 Object oriented Programming 22

Problem 1
• Three friends named Ram, Krish and Sam go to a
baker to buy some chocolates, cakes and
sandwiches. What is the amount paid by each
one of them?
(Note: The unit price of the items are fixed)

June 2021 Nalinadevi Kadiresan


19CSE204 Object oriented Programming 23

C program

June 2021 Nalinadevi Kadiresan


19CSE204 Object oriented Programming 24

Java program

June 2021 Nalinadevi Kadiresan


19CSE204 Object oriented Programming 25

Try this in C and Java


Monica Martin makes $10,000 per year at her part-time job. Her boss offers
Monica a raise but she must choose one of the following two options:
1.a thousand-dollar bonus right away followed by a ten percent salary
increase every year.
2.a salary increase of one-twelfth of ten percent each month (and no bonus).
Write a program to aid Monica in making her decision.  Display a three
column output that shows for each successive year the year number, and the
salary for each of the two options.  The program should terminate when the
salary for option two becomes greater than that for option one.

Note for Java code: write the program inside main method

June 2021 Nalinadevi Kadiresan

You might also like