Simple JAVA Problems & Solution | C (Programming Language)

July 3, 2017 | Author: Anonymous | Category: Java
Share Embed


Short Description

Psedo Code: class SampleOne { public static void main(String args[]) { System.out.println("Java is better than C++."); }...

Description

S. M. 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++."); } }

Problem No: 3.2 Problem Specification: a java program with multiple statements. Description of the problem: this java program is a simple java program. But it has more number of statements and in this program we use math Function. Psedo Code: 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); } }

Problem No: 3.3 Problem Specification: A program with multiple classes. Description of the problem: This program defines two classes Room and RoomArea. The Room class defines two variables and one method to assign value to these variables. The class RoomArea contains the main method that initiates the execution. We use the getdata method to assigns values to the data members of Room class. Psedo Code: class Room {

S. M. Rakibul Hasan ID: 14CSE070400 float length, breadth; void getdata(float a, float b) { length = a; breadth = b; } } 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); } }

Problem No: 3.4 Problem Specification: A simple program for testing. Description of the problem: The file name must be the classname of the class containing the main method. Psedo Code: 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."); } }

Problem No: 3.5 Problem Specification: Write a java program use of command line arguments.

S. M. Rakibul Hasan ID: 14CSE070400 Description of the problem: This program illustrates the use of command line arguments. Psedo Code: /* This program uses command line arguments as input.*/ class ComLineTest { public static void main(String args[]) { int count, i=0; String string; count = args.length; System.out.println("Number of arguments = " + count); while(i< count) { string = args[i]; i= i+1; System.out.println(i+ " : " + "Java is " +string+ "!"); } } }

Problem No: 4.1 Problem Specification: Reading data from keyboard. Description of the problem: The readLine( ) method reads the input from the keyboard as a string which is then converted to the integer data type using the data type wrapper class. We have used the keyword try and catch to handle any errors that might occur during the reading process. Psedo Code: 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"); } System.out.println("The integer number is : " + n); System.out.println("The float number is : " + f); System.out.println("The String is : " + s); } }

Problem No: 4.2 Problem Specification: Creation and casting of variables. Description of the problem: This is a simple java program which is the creation and casting of variable. Psedo Code: 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; System.out.println(" (short)b = " + s1); System.out.println(" (short)i = " + s2); System.out.println(" (float)l = " + n1); System.out.println(" (int)f1 = " + m1); } }

Problem No: 4.3 Problem Specification: Getting the result to the screen. Description of the problem: This program illustrates the behavior of print() and println() method. Psedo Code: class Displaying { public static void main(String args[]) { System.out.println("Screen Diplay"); for(int i =1; i=b)); System.out.println(" b!=c is " +(b!=c)); System.out.println(" b==a+c is " +(b==a+c)); } }

Problem No: 5.3 Problem Specification: Increment operator illustrated. Description of the problem: In this program we fallow the work of increment operator. Psedo Code: class IncrementOperator { public static void main(String args[]) { int m=10, n=20; System.out.println(" m = " +m); System.out.println(" n = " +n); System.out.println(" ++m = " + ++m); System.out.println(" n++ = " + n++); System.out.println(" m = " + m); System.out.println(" n = " + n); } }

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 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); } }

View more...

Comments

Copyright © 2017 DATENPDF Inc.