Java Exercises

Copy and compile this program in your IDE.

import java.io.*;
class input1{
public static void main(String args[ ]) throws IOException {
InputStreamReader numb = new InputStreamReader(System.in);
BufferedReader stndin = new BufferedReader(numb); //
System.out.print("Input any integer: "); //
String num = stndin.readLine();
int a= 53;
int b= a&4; //
System.out.flush(); //
int inum = Integer.parseInt(num);
System.out.print("Number \n" + b);//(inum * inum));
}
}

==========================================
import java.io.*;
class inputInteger {
public static void main(String args[]) throws IOException {
InputStreamReader a = new InputStreamReader(System.in);
// An InputStreamReader is a bridge from byte streams to
// character streams: It reads bytes and translates them into
// characters
BufferedReader stndin = new BufferedReader(a);
// Read text from a character-input stream, buffering
// characters so as to provide for the efficient reading of
// characters, arrays, and lines
System.out.print("Input first integer: ");
String n1=stndin.readLine();
System.out.flush();
System.out.print("Input second integer:");
String n2=stndin.readLine();
System.out.flush();
System.out.print("Input third integer:");
String n3=stndin.readLine();
System.out.flush();
//String num = stndin.readLine();
int num1 = Integer.parseInt(n1);
int num2 = Integer.parseInt(n2);
int num3 = Integer.parseInt(n3);
// Parses the string argument as a signed decimal integer.
// The characters in the string must all be decimal digits
// The resulting integer value is returned, exactly as if the
// argument and the radix 10 were given as arguments to the
// parseInt method. This method returns the integer
// represented by the argument in decimal.
int x=0,y=0;
if ((num1>num2) && (num1>num3)){
System.out.println("The largest number is:"+n1); x=num1; }
else if((num2>num1) && (num2>num3)){
System.out.println("The largest number is:"+n2); x=num2; }
else if((num3>num1) && (num3>num2)){
System.out.println("The largest number is:"+n3); x=num3; }
if ((num1 System.out.println("The smallest number is:"+n1); y=num1;
}
else if((num2 System.out.println("The smallest number is:"+n2); y=num2;
}
else if((num3 System.out.println("The smallest number is:"+n3);
y=num3;
}
System.out.print("The difference is " + (x-y));
}}

==============
import java.io.*;
public class largesmall {
public static void main(String args[]) throws IOException {
int n1, n2, n3, large, small;
InputStreamReader a = new InputStreamReader(System.in);
BufferedReader stndin = new BufferedReader(a);
System.out.print("Input any integer: ");
String s = stndin.readLine();
System.out.flush();
n1 = Integer.parseInt(s);
System.out.print("Input any integer: ");
s = stndin.readLine();
System.out.flush();
n2 = Integer.parseInt(s);
System.out.print("Input any integer: ");
s = stndin.readLine();
System.out.flush();
n3 = Integer.parseInt(s);
large = n1;
if (n2>large) large = n2;
if (n3>large) large = n3;
small = n1;
if (n2if (n3System.out.println("The value of n1 = " + n1);
System.out.println("The value of n2 = " + n2);
System.out.println("The value of n3 = " + n3);
System.out.println();
System.out.println("The largest = " + large);
System.out.println("The smallest = " + small);
}}

=================================
public class if0{
public static void main(String args[]){
int x=6;
if (x>0) System.out.println("Number is positive");
else System.out.println("Number is negative");

}}

=================================

public class if1{
public static void main(String args[]){
int x=0;
x=Integer.parseInt(args[0]);
if (x>0)
System.out.println("Number is positive");
else System.out.println("Number is negative");
}}

=================================
public class if2{
public static void main(String args[]){
int x=-5;
if (x>=0)
System.out.print("Number is zero or positive");
else
System.out.print("Number is negative");
}}

===================================
classname: DateFormat

Given as input three integers representing a date as day, month, year, print out the number day, month and year for the following day's date.
Typical input: 28 2 1992 Typical output: Date following 28:02:1992 is 29:02:1992

classname: Message

Write a program which reads two integer values. If the first is less than the second, print the message up. If the second is less than the first, print the message down If the numbers are equal, print the message equal If there is an error reading the data, print a message containing the word Error and perform exit( 0 );

Post a Comment

0 Comments