0% found this document useful (0 votes)
6 views10 pages

? Unit II Notes - Compiling and Executing Java Programs (BlueJ)

The document provides notes on compiling and executing Java programs using the BlueJ platform, covering topics such as writing, compiling, and executing Java code, as well as accepting user input during runtime. It explains assignment statements, flow control, conditional statements, and types of errors in programming. Additionally, it includes example programs demonstrating various concepts like simple interest, percentage calculations, and decision-making in Java.
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)
6 views10 pages

? Unit II Notes - Compiling and Executing Java Programs (BlueJ)

The document provides notes on compiling and executing Java programs using the BlueJ platform, covering topics such as writing, compiling, and executing Java code, as well as accepting user input during runtime. It explains assignment statements, flow control, conditional statements, and types of errors in programming. Additionally, it includes example programs demonstrating various concepts like simple interest, percentage calculations, and decision-making in Java.
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

📒 Unit II Notes: Compiling and

Executing Java Programs (BlueJ)

1. Java Programming on BlueJ Platform


Now we learn how to:

●​ Write Java programs


●​ Compile them
●​ Execute them on BlueJ
●​ Accept values from user during runtime

2. Using Assignment Statement


Assignment Statement:

It assigns a value to a variable.

Example:

int a = 10;
int b = 20;
sum = a + b;

This is one of the easiest ways to write beginner programs because values are directly given
inside the program.

Example Program: Sum and Difference


public class Sum
{
public static void main(String args[])
{
int a=345, b=276;
int sum=0, diff=0;

sum = a + b;
diff = a - b;

[Link]("First number = " + a);


[Link]("Second number = " + b);
[Link]("Sum = " + sum);
[Link]("Difference = " + diff);
}
}

3. Compilation and Execution in BlueJ


Steps:

1.​ Write program in editor


2.​ Click Compile
3.​ Message appears:

✅ Class compiled – no syntax errors


4.​ Right-click class icon
5.​ Select:

void main(String[] args)

6.​ Output appears in terminal window

4. Accepting Input Values using main()


Function
Instead of assigning values in program, we can input values at runtime.

Example:
public static void main(int p, int r, int t)

BlueJ will ask values in method call window.

Program: Simple Interest


public class Interest
{
public static void main(int p, int r, int t)
{
double si, amt;

si = (p*r*t)/100.0;
amt = p + si;

[Link]("Simple Interest = Rs." + si);


[Link]("Amount = Rs." + amt);
}
}

5. Program: Convert Days into Years,


Months, Days
public class Days
{
public static void main(int n)
{
int y, m, d;

y = n/365;
n = n%365;

m = n/30;
d = n%30;

[Link]("Years = " + y);


[Link]("Months = " + m);
[Link]("Days = " + d);
}
}
6. Program: Percentage of Boys and
Girls
If total students = n​
Girls = m

public class Percentage


{
public static void main(int n, int m)
{
float perb, perg;

perg = (float)m*100.0f/n;
perb = 100.0f - perg;

[Link]("Percentage of boys = " + perb);


[Link]("Percentage of girls = " + perg);
}
}

7. Flow of Control
Flow of Control:

Order in which statements are executed in a program.

Types:

1.​ Normal Flow of Control


2.​ Conditional Flow of Control

A. Normal Flow of Control


Statements execute one after another in sequence.

Example:

Statement 1 → Statement 2 → Statement 3 → Output


B. Conditional Flow of Control
Execution depends on conditions.

Decision making statements:

1.​ if statement
2.​ if–else statement
3.​ if–else–if ladder

8. if Statement
Used when a statement should execute only if condition is true.

Syntax:
if(condition)
statement;

9. if–else Statement
Executes one block if condition is true, otherwise executes else block.

Syntax:
if(condition)
statement1;
else
statement2;

10. Multiple if Statement


Used when there are multiple independent conditions.

Syntax:
if(condition1)
statement1;

if(condition2)
statement2;

if(condition3)
statement3;

11. if–else–if Ladder


Used when more than one condition is checked but only one will execute.

Syntax:
if(condition1)
statement1;
else if(condition2)
statement2;
else
statement3;

12. Types of Errors in a Program


There are three main types:

1. Syntax Error
Occurs when grammar rules of Java are not followed.

Example:

c = (a+b/2);

Correct:

c = (a+b)/2;
2. Logical Error
Program runs but gives wrong output due to incorrect logic.

Example:

Writing:

a*b

Instead of:

a+b

3. Run-time Error
Occurs during execution, cannot be detected by compiler.

Example:

Division by zero:

a = b/0;

13. Solved Programs using Decision


Making

Program 1: Square and Cube of Smaller/Greater


Number
if(a>b)
{
sq = b*b;
cb = a*a*a;
}
else
{
sq = a*a;
cb = b*b*b;
}

Output:

●​ Square of smaller number


●​ Cube of greater number

Program 2: Greatest of Three Numbers


if((a>b)&&(a>c))
[Link]("First is greatest");
else if((b>a)&&(b>c))
[Link]("Second is greatest");
else
[Link]("Third is greatest");

Program 3: Profit or Loss


if(sp>cp)
{
profit = sp-cp;
pp = profit*100.0/cp;
}
else if(cp>sp)
{
loss = cp-sp;
lp = loss*100.0/cp;
}
else
[Link]("Neither Profit nor Loss");

Program 4: Type of Triangle


Conditions:

●​ Triangle possible if:

(a+b>c)&&(b+c>a)&&(c+a>b)
Types:

●​ Equilateral: a=b=c
●​ Isosceles: any two sides equal
●​ Scalene: all different

Program 5: Grades Based on Average Marks


Average of 3 subjects:

avg = (p+c+b)/3.0;

Grade table:

Average Marks Grade

80% and above Distinction

60–79% First Division

45–59% Second Division

Below 45% Promotion not


granted

✅ Chapter at a Glance
●​ Programs can be designed by:
○​ Assigning values inside program
○​ Accepting input at runtime
●​ Flow of Control:
○​ Normal flow
○​ Conditional flow
●​ Conditional statements:
○​ if
○​ if–else
○​ if–else–if ladder
●​ Errors:
○​ Syntax error
○​ Logical error
○​ Run-time error

You might also like