Lab 2
Lab 2
LAB 2
INTRODUCTION TO OOP
LESSON OUTCOMES
At the end of the lab session, students should be able to:
BlueJ
A free Java Development Environment
designed for beginners, used by millions
worldwide.
C++ JAVA
using namespace std; public class welcome
#include <iostream> {
public static void main(String args[])
{
int main() System.out.println(“Everyone Can
{ Code");
cout<<“Everyone Can Code"; }
} }
PROGRAM # 2
To Add Two Numbers
C++ JAVA
using namespace std; import java.util.Scanner;
#include <iostream> public class displayValues
{
int main() public static void main(String args[])
{
{
int num1,num2;
int num1,num2; Scanner input=new Scanner (System.in);
System.out.println("Enter first number:");
cout<<"Enter first number:"; num1=input.nextInt();
cin>>num1; System.out.println("Enter second
cout<<"\nEnter second number:"; number:");
num2=input.nextInt();
cin>>num2; System.out.println("First and second
cout<<"\nFirst and second value is value is "+num1+" and "+num2);
"<<num1<<" and "<<num2; }
} }
PROGRAM # 2
import javax.swing.JOptionPane;
// Character input
char c = sc.next().charAt(0);
Question
BMI = weight(kg)/height(metres)
Hint :
to input float value:input.nextFloat();
to input double value:input.nextDouble();
QUESTION 1 (TUTORIAL 1) -ANSWER
C++ - Display the BMI by using the given formula:
Output Sample
QUESTION 1 (TUTORIAL 1) -ANSWER
Java - Display the BMI by using the given formula:
Output Sample
QUESTION 2 (REPETITION/LOOP)
Display the highest salary based on the 5 inputs
By using C++ and Java programming language but without using records or array,
write a program to input 5 names and their salary. Display the highest salary
together with its name.
Hint :
In java, to input names used String data type and input String by using ,
input.nextLine();
QUESTION 2 (REPETITION/LOOP) Answer
- C++ - Display the highest salary based on the 5 inputs
Output Sample
QUESTION 2 (REPETITION/LOOP) Answer
- Java - Display the highest salary based on the 5 inputs
Output Sample
C++ VS JAVA
Try on Your Own ☺
Question 1
Question 2
Question 3
Question 4