0% found this document useful (0 votes)
5 views42 pages

Java Compiler CG File

The document is a practical file for the MCA program at Sat Kabir Institute of Technology & Management, detailing various Java programming exercises. It includes objectives, problem analyses, algorithms, flow charts, and code examples for tasks such as printing 'Hello World', performing arithmetic operations, and checking number properties. The file is structured with an index and multiple practical sections covering different programming concepts and techniques.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views42 pages

Java Compiler CG File

The document is a practical file for the MCA program at Sat Kabir Institute of Technology & Management, detailing various Java programming exercises. It includes objectives, problem analyses, algorithms, flow charts, and code examples for tasks such as printing 'Hello World', performing arithmetic operations, and checking number properties. The file is structured with an index and multiple practical sections covering different programming concepts and techniques.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

SAT KABIR INSTITUTE OF TECHNOLOGY & MANAGEMENT

PRACTICAL FILE

( JAVA , COMPILER DESIGN AND COMPUTER GRAPHICS )

SESSION : November 2025

SUBMITTED TO SUBMITTED BY

Ms. Manju (HOD) Name-

(A.P. MCA DEPTT.) Semester- 1st sem

SKITM, LADRAWAN Dept- MCA

Roll No.-

MAHARSHI DAYANAND UNIVERSITY, ROHTAK


INDEX

LIST OF PROGRAMS

Sr. PROGRAM DATE SIGN.


No.

1. Simple program to print “Hello, World!”

2. Addition, Subtraction, Division and


Multiplication of two numbers

3. Program to find Even & Odd Number

4. Find greatest no. among given 3 no.

5. Program to find positive/ negative no.

6. To check whether string belong a grammar or not

7. To calculate leading of Non-Terminals of the


Given Grammar

8. To calculate trailing of Non-Terminals of the


Given Grammar

9. Program for computation of the first

10. Drawing & Filling Basic Graphics Primitives

11 2D Geometric Transformations & Clipping

12 3D Object Rendering (Wireframe) & Viewing

PRACTICAL - 1
Objective(s):
 To be familiar with syntax and structure of Java-programming.
 To learn problem solving techniques using Java.
 Java Input Output (I/O).
Title:
Write a program to print “HELLO WORLD”.
Problem Analysis:
This is a first program in Java language; this program will print the text (‘Hello World’) on the
output screen. Here, we are going to learn how to write, execute and run a first program in Java
programming language?
you have to include only single header file that is string, this header file contains the declaration
public modifier.
[Link] is used to display message as well as value on the standard output device
(monitor), use of println function is very easy, you have to just pass the string (message) that you
want to print on the screen within inverted commas ("message").

ALGORITHM:

1. BEGIN

2. PRINT "Hello World"

3. END

FLOW CHART:
CODE:
class Intro
{ public static void main(String args[])
{ [Link]("Hello, World");
}
}
Output (Compilation, Debugging & Testing):
Hello, World!
Discussion and Conclusion:
 The Public Static Void Main is a preprocessor command. This command tells compiler to
include the contents of (standard input and output) file in the program.

PRACTICAL - 2

Objective(s):
 Programming Code to Perform addition.
Title:
Write a Java program to perform SUM OF TWO NUMBERS TAKING INPUT FROM
USER
Problem Analysis:
To perform addition, subtraction, multiplication and division of any two numbers in Java
programming, you have to ask to the user to enter the two numbers and then ask to enter the
operator to perform the mathematical operations. Numbers are assumed to be integers and
will be entered by the user.
ALGORITHM:
To add two numbers:
1. Read the Value of A and B.
2. SUM = A+B.
3. Display SUM.
4. Stop.
Similarly to Multiply two numbers.
1. Read the Value of A and B.
2. PRODUCT = A*B.
3. Display PRODUCT.
4. Stop.
For Divide two numbers.
1. Read the Value of A and B.
2. DIV = A/B.
3. Display DIV.
4. Stop.

For Subtract two Numbers.


1. Read the Value of A and B.
2. SUB = A-B.
3. Display SUB.
4. Stop.
FLOW CHART:

CODE:
import [Link].*; //Courage Coded it class Add twonumbs
{ public static void main(String args[])
{ int a,b,c; Scanner s=new Scanner([Link]);
[Link]("Enter two numbers");
a=[Link]();
b=[Link]();
c=a+b;
[Link]("The Sum is: "+c);
}}
Output (Compilation, Debugging & Testing):
Discussion and Conclusion:
In Java language when we divide two integers we get an integer as a result, for example, 44 and
65 evaluates to 109. As a general rule integer/integer = integer and float/integer = float or
integer/float = float. So we convert denominator to float in our program, you may also write float
in the numerator. This explicit conversion is known as typecasting and type conversion is also in
java.
PRACTICAL - 3
OBJECTIVE(S):
 To learn problem solving techniques using Java through if –else.
TITLE:
Write a Java Program to Check Whether a Number is Greater than the Other.
PROBLEM ANALYSIS:
Even numbers are perfectly divisible by 2. In this example, if-else statement is used to check
whether a number entered by the user is even or odd.
FLOW CHART:

ALGORITHM:
Step 1 : Start
Start 2 : Read n
Start 3 : if (n%2 == 0), Write which number is greater"
Start 4 : Stop

CODE:
import [Link].*;
class Greatest
{
public static void main(String args[]){

int numb1,numb2,numb3;
Scanner s=new Scanner([Link]);
[Link]("Welcome, Please Enter three numbers to try");
numb1=[Link]();
numb2=[Link]();
numb3=[Link]();
if(numb1>=numb2){
if(numb1>numb3){
[Link]("The Number "+numb1+"is Greatest!");
}
Else
{
[Link]("The Number "+numb3+"is Greatest!");
}
}
if(numb2>numb1){
if(numb2>numb3){
[Link]("The Number "+numb2+"is Greatest!");
}
else{
[Link]("The Number "+numb3+"is Greatest!");
}
}
}
}

GREATER AMONG THREE NUMBERS


OUTPUT (COMPILATION, DEBUGGING & TESTING):
PRACTICAL - 4
Objective(s):
 To learn problem solving techniques using CALCULATING THE RESULT OF 5
SUBJECTS
Title:
Write a program to find & print calculating the result of 5 subjects.
Problem Analysis:
This program takes the different Subjects marks.

CODE:
import [Link].*;
class StudentMarks
{
public static void main(String args[]){
int Tmarks,sub1,sub2,sub3,sub4,sub5;
float percentage;
String name,rollnum,course;
Scanner s=new Scanner([Link]);
//Getting user's name, rollnumber and class
[Link]("Please Enter student's name");
name=[Link]();
[Link]("Please Enter student's Rollnumber");
rollnum=[Link]();
[Link]("Please Enter student's class");
course=[Link]();
//Getting marks from user
[Link]("Welcome "+name);
[Link]("Please Enter the Marks");
sub1=[Link]();
sub2=[Link]();
sub3=[Link]();
sub4=[Link]();
sub5=[Link]();
//Calculating Total marks and percentage
Tmarks=sub1+sub2+sub3+sub4+sub5;
percentage=Tmarks/5;
//Trying for failure
if(sub1<40||sub2<40||sub3<40||sub4<40||sub5<40)
{
[Link]("Failed!!! ");
}
if (percentage>=40&&percentage<60)
{
[Link]("Student Name: "+name);
[Link]("Student Class: "+course);
[Link]("Student RollNumber: "+rollnum);
[Link]("Obtained toal marks: "+Tmarks);
[Link]("Obtained Percentage:"+percentage);
[Link]("-------------------------------------------");
[Link]("Student Grade: AVERAGE! ");
}
if (percentage>=60&&percentage<70)
{
[Link]("Student Name: "+name);
[Link]("Student Class: "+course);
[Link]("Student RollNumber: "+rollnum);
[Link]("Obtained toal marks: "+Tmarks);
[Link]("Obtained Percentage:"+percentage);
[Link]("-------------------------------------------");
[Link]("Student Grade: GOOD! ");
}
if (percentage>=70&&percentage<80)
{
[Link]("Student Name: "+name);
[Link]("Student Class: "+course);
[Link]("Student RollNumber: "+rollnum);
[Link]("Obtained toal marks: "+Tmarks);
[Link]("Obtained Percentage:"+percentage);
[Link]("-------------------------------------");
[Link]("Student Grade: VERY GOOD! ");
}
if (percentage>=80)
{
[Link]("Student Name: "+name);
[Link]("Student Class: "+course);
[Link]("Student RollNumber: "+rollnum);
[Link]("Obtained toal marks: "+Tmarks);
[Link]("Obtained Percentage:"+percentage);
[Link]("-------------------------------------");
[Link]("Student Grade: EXCELLENT! ");
}
}}
Output:

PRACTICAL - 5
Objective(s):
 To learn problem solving techniques THE SUM OF FIRST N NATURAL NUMBERS
USING FOR LOOP.
Title:
Write a program to find first n natural numbers using for loop.
Problem Analysis:
Logic of this program is simple if you know basic maths. Let us have a quick look about number
properties.
 A number is said negative if it is less than 0 i.e. num< 0.
 A number is said positive if it is greater than 0 i.e. num> 0.
We will use the above logic inside if to check number for negative, positive or zero.
ALGORITHM:
1. BEGIN
2. Input a number from user in some variable say num.
3. Check if(num< 0), then number is negative.
4. Check if(num> 0), then number is positive.
5. Check if(num == 0), then number is zero.
6. END

FLOW CHART:
CODE:
package sumofnnaturalnumbers;
/** Coded it *
/ import [Link].*;
public class SumofNnaturalNumbers
{ /** * @param args the command line arguments
*/ public static void main(String[] args)
{ [Link]
("Welcome\nPlease Enter the nth number to sum");
Scanner s=new Scanner([Link]);
int n=[Link]();
int sum=0; for(int i=0;i<=n;i++)
{ sum+=i; }
[Link]("The sum of first nth numbers is: "+sum);
}
}
Output (Compilation, Debugging & Testing):

PRACTICAL 6

AIM:- TO CHECK WHETHER STRING BELONGS TO GRAMMAR OR NOT

PrOGRAM:-

#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include<string.h>
void main()
{
int a=0,b=0,c,d;
char str[20],tok[11];
printf("Input the expression = ");
gets(str);
while(str[a]!='\0')
{
if((str[a]=='(')||(str[a]=='{'))
{
tok[b]='4';
b++;
}
if((str[a]==')')||(str[a]=='}'))
{
tok[b]='5';
b++;
}
if(isdigit(str[a]))
{
while(isdigit(str[a]))
{
a++;
}
a--;
tok[b]='6';
b++;
}
if(str[a]=='+')
{
tok[b]='2';
b++;
}
if(str[a]=='*')
{
tok[b]='3';
b++;
}
a++;
}
tok[b]='\0';
puts(tok);
b=0;
while(tok[b]!='\0')
{
if(((tok[b]=='6')&&(tok[b+1]=='2')&&(tok[b+2]=='6'))||((tok[b]=='6')&&(tok[b+1
]=='3')&&(tok[b+2]=='6'))||((tok[b]=='4')&&(tok[b+1]=='6')&&(tok[b+2]=='5'))/*||((tok[b
]!=6)&&(tok[b+1]!='\0'))*/)
{
tok[b]='6';
c=b+1;
while(tok[c]!='\0')
{
tok[c]=tok[c+2];
c++;
}
tok[c]='\0';
puts(tok);
b=0;
}
else
{
b++;
puts(tok);
}}
d=strcmp(tok,"6");
if(d==0)
{
printf("It is in the grammar.");
}
else
{
printf("It is not in the grammar.");
}
getch();
}

output:-
PRACTICAL 7

AIM:- TO Calculate Leading Of Non-Terminals Of The Given


Grammar

PrOGRAM:-
#include<conio.h>
#include<stdio.h>
char arr[18][3] =
{
{'E','+','F'},{'E','*','F'},{'E','(','F'},{'E',')','F'},{'E','i','F'},{'E','$','F'},
{'F','+','F'},{'F','*','F'},{'F','(','F'},{'F',')','F'},{'F','i','F'},{'F','$ ','F'},
{'T','+','F'},{'T','*','F'},{'T','(','F'},{'T',')','F'},{'T','i','F'},{'T','$','F'},
};
char prod[6] = "EETTFF";
char res[6][3]=
{
{'E','+','T'},{'T','\0'},
{'T','*','F'},{'F','\0'},
{'(','E',')'},{'i','\0'},
};
char stack [5][2];
int top = -1;
void install(char pro,char re)
{
int i;
for(i=0;i<18;++i)
{
if(arr[i][0]==pro && arr[i][1]==re)
{
arr[i][2] = 'T';
break;
}
}
++top;
stack[top][0]=pro;
stack[top][1]=re;
}
void main()
{
int i=0,j;
char pro,re,pri=' ';
clrscr();

for(i=0;i<6;++i)
{
for(j=0;j<3 && res[i][j]!='\0';++j)
{
if(res[i][j]
=='+'||res[i][j]=='*'||res[i][j]=='('||res[i][j]==')'||res[i][j]=='i'||res[i][j]=='$')
{
install(prod[i],res[i][j]);
break;
}
}
}
while(top>=0)
{
pro = stack[top][0];
re = stack[top][1];
--top;
for(i=0;i<6;++i)
{
if(res[i][0]==pro && res[i][0]!=prod[i])
{
install(prod[i],re);
}}}
for(i=0;i<18;++i)
{
printf("\n\t");
for(j=0;j<3;++j)
printf("%c\t",arr[i][j]);
}
getch();
clrscr();
printf("\n\n");
for(i=0;i<18;++i)
{
if(pri!=arr[i][0])
{
pri=arr[i][0];
printf("\n\t%c -> ",pri);
}
if(arr[i][2] =='T')
printf("%c ",arr[i][1]);
}
getch();
}

output:-
PRACTICAL 8

AIM:- TO Calculate Trailing Of Non-Terminals Of The Given

Grammar

PrOGRAM:-

#include<conio.h>

#include<stdio.h>

char arr[18][3] =

{'E','+','F'},{'E','*','F'},{'E','(','F'},{'E',')','F'},{'E','i','F'},{'E','$','F'},

{'F','+','F'},{'F','*','F'},{'F','(','F'},{'F',')','F'},{'F','i','F'},{'F','$ ','F'},

{'T','+','F'},{'T','*','F'},{'T','(','F'},{'T',')','F'},{'T','i','F'},{'T','$','F'},

};

char prod[6] = "EETTFF";

char res[6][3]=

{
{'E','+','T'},{'T','\0','\0'},

{'T','*','F'},{'F','\0','\0'},

{'(','E',')'},{'i','\0','\0'},

};

char stack [5][2];

int top = -1;

void install(char pro,char re)

int i;

for(i=0;i<18;++i)

if(arr[i][0]==pro && arr[i][1]==re)

arr[i][2] = 'T';

break;

++top;

stack[top][0]=pro;

stack[top][1]=re;

void main()

int i=0,j;

char pro,re,pri=' ';


clrscr();

for(i=0;i<6;++i)

for(j=2;j>=0;--j)

if(res[i][j]=='+'||res[i][j]=='*'||res[i][j]=='('||res[i][j]==')'||res[i][j]=='i'||res[i][j]=='$')

install(prod[i],res[i][j]);

break;

else if(res[i][j]=='E' || res[i][j]=='F' || res[i][j]=='T')

if(res[i][j-1]=='+'||res[i][j-1]=='*'||res[i][j-1]=='('||res[i][j-

1]==')'||res[i][j-1]=='i'||res[i][j-1]=='$')

install(prod[i],res[i][j-1]);

break;

}}}}

while(top>=0)

pro = stack[top][0];

re = stack[top][1];

--top;

for(i=0;i<6;++i)

{
for(j=2;j>=0;--j)

if(res[i][0]==pro && res[i][0]!=prod[i])

install(prod[i],re);

break;

else if(res[i][0]!='\0')

break;

}}}

for(i=0;i<18;++i)

printf("\n\t");

for(j=0;j<3;++j)

printf("%c\t",arr[i][j]);

getch();

clrscr();

printf("\n\n");

for(i=0;i<18;++i)

if(pri!=arr[i][0])

pri=arr[i][0];

printf("\n\t%c -> ",pri);


}

if(arr[i][2] =='T')

printf("%c ",arr[i][1]);

getch();

output:-
PRACTICAL 9

AIM:- Program For Computation Of The First.

PrOGRAM:-

#include<stdio.h>

#include<conio.h>

#include<string.h>

void main()

char t[5],nt[10],p[5][5], first[5][5],temp;

int i,j,not,nont,k=0,f=0;

clrscr();

printf("\nEnter the no. of Non-terminals in the grammer:");


scanf("%d",&nont);

printf("\nEnter the Non-terminals in the grammer:");

for(i=0;i<nont;i++)

scanf("\n%c",&nt[i]);

printf("\nEnter the no. of Terminals in the grammer: ( Enter e for absiline ) ");

scanf("%d",&not);

printf("\nEnter the Terminals in the grammer:");

for(i=0;i<not||t[i]=='$';i++)

scanf("\n%c",&t[i]);

for(i=0;i<nont;i++)

p[i][0]=nt[i];

first[i][0]=nt[i];

printf("\nEnter the productions :");

for(i=0;i<nont;i++)

scanf("%c",&temp);

printf("\nEnter the production for %c ( End the production with '$' sign ):",p[i][0]);

for(j=0;p[i][j]!='$';)

{
j+=1;

scanf("%c",&p[i][j]);

}}

for(i=0;i<nont;i++)

printf("\nThe production for %c -> ",p[i][0]);

for(j=1;p[i][j]!='$';j++)

printf("%c",p[i][j]);

for(i=0;i<nont;i++)

f=0;

for(j=1;p[i][j]!='$';j++)

for(k=0;k<not;k++)

if(f==1)

break;

if(p[i][j]==t[k])

first[i][j]=t[k];

first[i][j+1]='$';

f=1;
break;

else if(p[i][j]==nt[k])

first[i][j]=first[k][j];

if(first[i][j]=='e')

continue;

first[i][j+1]='$';

f=1;

break;

for(i=0;i<nont;i++)

printf("\n\nThe first of %c -> ",first[i][0]);

for(j=1;first[i][j]!='$';j++)

printf("%c\t",first[i][j]);

getch();

}
output:-

PRACTICAL 10

Drawing & Filling Basic Graphics Primitives

Objective: To draw basic primitives (point, line, circle, ellipse) and apply filled-area algorithms
(scan-line polygon fill and flood-fill) as per Unit-II of the syllabus.

Tools/Environment: Use any graphics library in C/C++ (for example, the BGI Graphics.h
library) or any modern equivalent (SDL, OpenGL, etc.).

Procedure:

1. Initialize graphics mode.

2. Draw a set of primitives:

A point at coordinate (x₁, y₁)

A line between (x₂, y₂) and (x₃, y₃) using e.g. DDA or Bresenham’s line algorithm

A circle with centre (x₄, y₄) and radius r using the midpoint circle algorithm

An ellipse with centre (x₅, y₅) and radii a and b using the midpoint ellipse algorithm
3. Draw a polygon (for example a pentagon) and fill it using the scan-line polygon fill algorithm.

4. Choose another area (for example a closed irregular shape) and apply the flood-fill algorithm
(either boundary fill or flood fill) to fill it with a selected colour.

5. Display the filled shapes, allow the user to choose the fill-colour.

Expected Output: A window showing the drawn primitives, a filled polygon (with scan-line fill),
and a second shape filled using flood-fill (colour selectable).

Explain briefly how the midpoint circle algorithm works.

What is the difference between scan-line polygon fill and flood-fill?

Which algorithm is more efficient for filling large areas and why?

Modify the program to accept user input for the polygon vertices and fill-colour

Points for Practical File: Include screenshot of output, code listing, output description, flowchart
of the fill-algorithm, and comments on what you learnt.
PRACTICAL 11

2D Geometric Transformations & Clipping


Objective: To implement 2D transformations (translation, rotation, scaling, reflection) on a
geometric object, and perform clipping using Cohen-Sutherland or Liang-Barsky algorithm.

Procedure:

1. Draw a basic object – for example a house-shaped polygon (rectangle + triangle roof).

2. Provide menu options for transformations:

Translation by (tx, ty)

Rotation about origin by angle θ

Scaling by factors (sx, sy)

Reflection about x-axis, y-axis or origin

3. After applying each transformation, clear the screen and redraw the transformed object
(preferably in different colour).

4. Implement a window (clipping region) defined by user coordinates (xmin, ymin, xmax, ymax).

5. Clip the transformed object against the window using the Cohen–Sutherland line-clipping
algorithm (for each line segment in the object), and display only the visible portions.

Expected Output: A window with original object, then after each transformation display the new
object; after enabling clipping, only the part of object inside the window is visible.

Write the transformation matrices used for rotation and scaling.

How does the Cohen–Sutherland algorithm decide whether a line is trivially accepted, rejected or
partially accepted?

If you reflect an object twice (first about x-axis then y-axis), what is the net effect? Explain.

Modify the program to allow rotation about an arbitrary point (not origin).
Points for Practical File: Include code comments explaining each transformation, screenshot of
each step, flowchart of clipping algorithm, the transformation matrix derivations, reflections.

PRACTICAL 12

3D Object Rendering (Wireframe) & Viewing

Objective: To create a simple 3D object (for example a cube or a pyramid), apply basic
transformations (translation, rotation in 3D), and display it as a wireframe using simple
projection (orthographic or perspective). Optionally implement hidden line removal (if possible).

Procedure:

1. Define a 3D object: e.g., cube with vertices (±1,±1,±1) or pyramid with base and apex.

2. Apply transformations in 3D: translation (tx, ty, tz), rotation about x-axis, y-axis and z-axis by
angles α, β, γ; scaling (sx, sy, sz). Use 3D transformation matrices.

3. Choose a projection method: either orthographic projection (dropping z-coordinate) or


perspective projection (with a viewpoint). Apply the projection to get 2D screen coordinates.

4. Draw the wireframe of the projected object. Optionally implement simple hidden‐line removal
(for example by not drawing edges which are back-facing).

Expected Output: A 2D display showing the projected 3D object (cube/pyramid). User interacts
(menu) to rotate object about different axes, translate, scale and the object updates accordingly.

Write the 4×4 transformation matrix for rotation about z-axis in 3D.
What difference does perspective projection make compared to orthographic? Explain with
example.

How can you detect which faces/edges are hidden in a wireframe mod

Modify the program to implement different viewpoint positions (eye at different locations) and
observe the effect

You might also like