RegisterForgot Your Password?

Java Codes

A complete portal of Java Codes for budding Engineers More »


Program in Java to construct a Toy Compiler

Program to construct Toy Compiler

Theory :
What is Toy Compiler ?

A Compiler is an “INTERPRETER” which takes the instructions from user and converts them into machine language (binary language of  0′s and 1′s) ,performs the calculations and operations according to these instructions and finally produces an output to the user in readable format (English).The Toy C compiler has been built using lexing and parsing tools Flex and Bison.  The compiler generates code to a file as soon as all the information needed about each language construct is available (i.e., after parsing and static checking). There is no need to construct an abstract syntax tree .


Code :

import java.util.*;
import java.util.regex.*;

class ToyCompiler {
static String input;
static List<String> tokenList;
static String[] tokens;
static int ptr;
static List<String> floatVariables;
static List<String> intVariables;
static Map<Integer, String> variableMap;
static int expressionType;
static int ctr;

Calculate area and volume using Java

Create a class Shape which consists one final method area () and volume ().Create three subclasses Rect, Circle and Triangle and calculate area and volume of it

Code :

abstract class Shape
final void area()
System.out.println(“Final Method Can not be Overriden”);

Code to demonstrate swings

Write a Java program which will create a frame if we try to close it, it should change it’s color and it remains visible on the screen(Use swing).

Code :

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

Program for calculator (Java)

Write a Java program to simulate the arithmetic calculator (+ ,- ,* , /). Also add 2 buttons namely ‘on’ to start the calculator and ‘clear’ to clear the text box.
Display appropriate error message in the text box

Code :

import java.awt.*;
import java.awt.event.*;

Code for accepting strings through command line and performing operations on them (Java)

Write a program in Java to accept name of cities from the user and sort them in ascending order (Use Command Line Arguments)

Code :

class Ass14
public static void main(String args[])
int n=args.length;
int i,j;

Code for accepting numbers through command line and performing operations on them (Java)

Write a java Program to accept ‘n’ no’s through the command line and store all the prime no’s and perfect no’s into the different arrays and display both the arrays.



Code for employee details using abstract class

Create an abstract class Person. Derive two classes Employee and Worker from it. Use proper method to accept and display the details for the same.
The fields of Employee are Emp_no,Emp_name and address. Similar fields for worker are name and working hours



File handing in java

Write a Java program to create 2 files F1 and F2.Copy the contents of file F1 by changing the case into file F2.
F2 = F1
Also copy the contents of F1 and F2 in F3.
F3 = F1+F2
Display the contents of F3.
(Use Command Line Arguments. )