Simple JAVA Problems & Solution
Simple JAVA Problems & Solution
Rakibul Hasan
ID: 14CSE070400
Problem No: 3.1
Problem Specification: A simple Java program.
Description of the problem: This program is perhaps the simplest of all java programs.
This program contains class, Opening Brace, Main method, println method, Ending
Brace.
Psedo Code:
class SampleOne
{
public static void main(String args[])
{
System.out.println("Java is better than C++.");
}
}
import java.lang.Math;
class SquareRoot
{
public static void main(String args[])
{
double x = 5, y;
y = Math.sqrt(x);
System.out.println("y = " + y);
}
}
class Room
{
S. M. Rakibul Hasan
ID: 14CSE070400
float length, breadth;
class RoomArea
{
public static void main(String args[])
{
float area;
Room room1 = new Room();
room1.getdata(14, 10);
area = room1.length * room1.breadth;
System.out.println("Area = " + area);
}
}
class Test
{
public static void main(String args[])
{
System.out.println("Hello");
System.out.println("Welcome to the world of Java");
System.out.println("Let us learn Java.");
}
}
import java.io.*;
class Reading
{
public static void main(String args[])
{
DataInputStream in = new DataInputStream(System.in);
int n=0;
float f=0.0F;
String s="";
S. M. Rakibul Hasan
ID: 14CSE070400
try
{
System.out.println("Enter an integer");
n=Integer.parseInt(in.readLine());
System.out.println("Enter a float");
f=Float.parseFloat(in.readLine());
System.out.println("Enter a string");
InputStreamReader b = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(b);
s=br.readLine();
}
catch(Exception e)
{
System.out.println("I/O Error");
}
class TypeWrap
{
public static void main(String args[])
{
System.out.println("Variables created");
char c = 'X';
byte b = 50;
short s = 1996;
int i = 123456789;
long l = 1234567654321L;
float f1 = 3.142F;
float f2 = 1.2e-5F;
double d2 = 0.000000987;
S. M. Rakibul Hasan
ID: 14CSE070400
System.out.println(" c = " + c);
System.out.println(" b = " + b);
System.out.println(" s = " + s);
System.out.println(" i = " + i);
System.out.println(" l = " + l);
System.out.println(" f1 = " + f1);
System.out.println(" f2 = " + f2);
System.out.println(" d2 = " + d2);
System.out.println("");
System.out.println("Types converted");
short s1 = (short)b;
short s2 = (short)i;
float n1 = (float)l;
int m1 = (int)f1;
class Displaying
{
public static void main(String args[])
{
System.out.println("Screen Diplay");
for(int i =1; i<=9; i++)
{
for(int j = 1; j<= i;j++)
{
System.out.print(" ");
System.out.print(i);
}
System.out.print("\n");
S. M. Rakibul Hasan
ID: 14CSE070400
}
System.out.println("Screen Display Done");
}
}
class FloatPoint
{
public static void main(String args[])
{
class RelationalOperators
{
public static void main(String args[])
{
S. M. Rakibul Hasan
ID: 14CSE070400
class IncrementOperator
{
public static void main(String args[])
{
}
}
S. M. Rakibul Hasan
ID: 14CSE070400
Problem No: 5.4
Problem Specification: Illustration of the use of casting operation.
Description of the problem: This is a simple java program which are illustrates use
casting operation.
Psedo Code:
class Casting
{
public static void main(String args[])
{
int i;
float sum;
sum=0.0F;
for(i=1;i<=10;i++)
{
sum = sum + 1/(float)i;
System.out.print(" i = " + i);
System.out.println(" sum = " + sum);
}
}
}
class ExpressWrap
{
public static void main(String args[])
{
//Order of Evaluation
int answer1 = a * b + c / d;
int answer2 = a * (b + c) / d;
//Type Conversions
float answer3 = a / c;
float answer4 = (float)a / c;
float answer5 = a / y;
S. M. Rakibul Hasan
ID: 14CSE070400
//Modulo Operations
int answer6 = a % c;
float answer7 = x % y;
//Logical Operations
boolean bool1 = a > b && c > d;
boolean bool2 = a < b && c > d;
boolean bool3 = a < b || c > d;
boolean bool4 = !(a-b == c);
System.out.println("Order of Evaluation");
System.out.println(" a * b + c / d = " + answer1);
System.out.println(" a * (b + c) / d = " + answer2);
System.out.println("Type Conversion");
System.out.println(" a / c = " + answer3);
System.out.println(" (float)a / c = " + answer4);
System.out.println(" a / y = " + answer5);
System.out.println("Modulo Operation");
System.out.println(" a % c = " + answer6);
System.out.println(" x % y = " + answer7);
System.out.println("Logical Operations");
System.out.println(" a > b && c > d = " + bool1);
System.out.println(" a < b && c > d = " + bool2);
System.out.println(" a < b || c > d = " + bool3);
System.out.println(" !(a-b == c) = " + bool4);
}
}