Remember
me?
RegisterForgot Your Password?

Java Codes

A complete portal of Java Codes for budding Engineers More »

 

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).

 

 Program to 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  java . io.*;
import  javax . swing.*;
class sframe  extends  JFrame

{

Code to develop arithmetic calculator in java

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

Explaination for the code :

1)We are creating a frame and adding panels to it .The panels are holding our controls like buttons and text fields .

2)ActionListener interface is used which will capture the actions on our buttons .The addActionListener() method will give implicit call to actionPerformed() which consists the corresponding functionality for each button.

3)We have used BorderLayout which sets the panels in specific directions.

 

Code :

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

Accept name of cities from the user and sort them in ascending order (Use Command Line Arguments)

Java program for Sorting the names of cities taken as input from command-line .

Description : 

1)   The following code takes the input from user’s command line which is held by the args variable . Follow these steps to execute the code —-

javac  Ass14.java

java  Ass14   NewYork  Mumbai  Chicago

 

2) Here’s the code :

class Ass14

{
public static void main(String args[])
{
int n=args.length;

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.*;
import java.io.*;

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 java.io.*;
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;