0% found this document useful (0 votes)
18 views21 pages

Lab 2

This document serves as a lab guide for students to learn Java programming using BlueJ, focusing on object-oriented programming (OOP) concepts. It includes examples comparing C++ and Java for input/output operations, basic arithmetic, and data type handling, along with exercises to reinforce learning. Students are encouraged to write their own programs based on provided prompts to further their understanding of Java.

Uploaded by

johan razak
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views21 pages

Lab 2

This document serves as a lab guide for students to learn Java programming using BlueJ, focusing on object-oriented programming (OOP) concepts. It includes examples comparing C++ and Java for input/output operations, basic arithmetic, and data type handling, along with exercises to reinforce learning. Students are encouraged to write their own programs based on provided prompts to further their understanding of Java.

Uploaded by

johan razak
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

GETTING STARTED WITH BLUEJ & JAVA

LAB 2
INTRODUCTION TO OOP
LESSON OUTCOMES
At the end of the lab session, students should be able to:

▪ Differentiate the input and output statement in C++ and Java


Programming Language.

▪ Write a simple program in Java Programming Language.


GETTING STARTED WITH BLUEJ
Website (to download) – h t t p s : / / w w w. b l u e j . o r g /

BlueJ
A free Java Development Environment
designed for beginners, used by millions
worldwide.

The editor allows us to develop Java


programs quickly and easily.
GETTING STARTED WITH
BLUEJ BlueJ Main Window
PROGRAM 1
To Display a Message

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;

public class InputOutputDialogBox


{
public static void main(String args[])
{
String input;
int num1,num2;
input = JOptionPane.showInputDialog("Enter first number :");
num1 = Integer.parseInt(input);

input = JOptionPane.showInputDialog("Enter second number :");


num2 = Integer.parseInt(input);

JOptionPane.showMessageDialog(null, "num1 : "+num1+" num2 : "+num2);


}
}
PROGRAM # 2
To Add Two Numbers & Display Sum , Average
JAVA
import java.util.Scanner;
public class displaySumAvg
{
public static void main(String args[])
{ Scanner inputNum=new Scanner(System.in);
int num1,num2,sum,avg;
System.out.println("Enter first number:");
num1=inputNum.nextInt();
System.out.println("Enter second number:");
num2=inputNum.nextInt();
sum=num1+num2;
avg=sum/2;
System.out.println("Sum value:"+sum+"\nAverage value:"+avg);
}
}
PROGRAM # 3
To Input Different Data Types
import java.util.Scanner;
public class diffInput
{ public static void main(String args[ ])
{ Scanner inputText=new Scanner(System.in);//to input text
Scanner input=new Scanner(System.in);//to input numbers
String name;
int age;
double weight;
System.out.println("Enter name:");
name=inputText.nextLine();
System.out.println("Enter age:");
age=input.nextInt();
System.out.println("Enter weight:");
weight=input.nextDouble();

System.out.println("Your name is "+name+" and your age is "+age+” with


weight “+weight);
}
}
INPUT TYPES
METHOD DESCRIPTION
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() or next() Reads a String value from the user
nextLong() Reads a long value from the user
nextShort() Reads a short value from the user
PROGRAM # 4
To Input A Character
// Java program to read character using Scanner class
import java.util.Scanner;
public class ScannerDemo1
{ public static void main(String[] args)
{
// Declare the object and initialize with
// predefined standard input object
Scanner sc = new Scanner(System.in);

// Character input
char c = sc.next().charAt(0);

// Print the read value


System.out.println("c = "+c);
}
}
GETTING STARTED WITH JAVA
Try on Your O w n ☺

Question

By using java programming language, write a program that will input in


kilograms and height in metres. Display the BMI by using the given
formula:

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

Write an application that inputs three integers from the


user and displays the sum, average, product, smallest and
largest of the numbers.
C++ VS JAVA
Try on Your Own ☺

Question 2

Write a program that does the following:


a) Get the number of integer value from user.
b) Loop and allow user to enter the random of integer value
and determine the number is odd or even.
c) Count the number of even and odd values.
d) Print the number of even and odd values.
C++ VS JAVA

Question 3

Write a program (Java) that converts a Fahrenheit degree to


Celsius using the following formula. Get the Fahrenheit value from user.
celsius = (fahrenheit – 32) x 5 / 9
You may switch your program by receiving the Celsius value and convert
to Fahrenheit.
fahrenheit = celsius x 9 / 5 + 32
C++ VS JAVA
Try on Your Own ☺

Question 4

Write a Java program that reads 5 student


names and their CSC186 Test 1 marks. Display
the highest marks together with its name.

You might also like