a) Using javap, view the methods of the following classes from the lang package:
java.lang.Object , java.lang.String and java.util.Scanner and also Compile sample
program 8. Type the following command and view the bytecodes. javap -c MyClass.
Command : javap java.lang.Object
Output:
javap java.lang.Object
Compiled from "Object.java" public class java.lang.Object { public java.lang.Object(); public final native java.lang.Class<?> getClass(); public native int hashCode(); public boolean equals(java.lang.Object); protected native java.lang.Object clone() throws java.lang.CloneNotSupportedException; public java.lang.String toString(); public final native void notify(); public final native void notifyAll(); public final void wait() throws java.lang.InterruptedException; public final native void wait(long) throws java.lang.InterruptedException; public final void wait(long, int) throws java.lang.InterruptedException; protected void finalize() throws java.lang.Throwable; static {}; }
Command : javap java.lang.String
Output:
Compiled from "String.java" public final class java.lang.String implements java.io.Serializable, java.lang.Comparable<java.lang.String>, java.lang.CharSequence { static final boolean COMPACT_STRINGS; public static final java.util.Comparator<java.lang.String> CASE_INSENSITIVE_ORDER; static final byte LATIN1; static final byte UTF16; public java.lang.String(); public java.lang.String(java.lang.String); public java.lang.String(char[]); public java.lang.String(char[], int, int); public java.lang.String(int[], int, int); public java.lang.String(byte[], int, int, int); public java.lang.String(byte[], int); public java.lang.String(byte[], int, int, java.lang.String) throws java.io.UnsupportedEncodingException; public java.lang.String(byte[], int, int, java.nio.charset.Charset); public java.lang.String(byte[], java.lang.String) throws java.io.UnsupportedEncodingException; public java.lang.String(byte[], java.nio.charset.Charset); public java.lang.String(byte[], int, int); public java.lang.String(byte[]); public java.lang.String(java.lang.StringBuffer); public java.lang.String(java.lang.StringBuilder); public int length(); public boolean isEmpty(); public char charAt(int); public int codePointAt(int); public int codePointBefore(int); public int codePointCount(int, int); public int offsetByCodePoints(int, int); public void getChars(int, int, char[], int); public void getBytes(int, int, byte[], int); public byte[] getBytes(java.lang.String) throws java.io.UnsupportedEncodingException; public byte[] getBytes(java.nio.charset.Charset); public byte[] getBytes(); public boolean equals(java.lang.Object); public boolean contentEquals(java.lang.StringBuffer); public boolean contentEquals(java.lang.CharSequence); public boolean equalsIgnoreCase(java.lang.String); public int compareTo(java.lang.String); public int compareToIgnoreCase(java.lang.String); public boolean regionMatches(int, java.lang.String, int, int); public boolean regionMatches(boolean, int, java.lang.String, int, int); public boolean startsWith(java.lang.String, int); public boolean startsWith(java.lang.String); public boolean endsWith(java.lang.String); public int hashCode(); public int indexOf(int); public int indexOf(int, int); public int lastIndexOf(int); public int lastIndexOf(int, int); public int indexOf(java.lang.String); public int indexOf(java.lang.String, int); static int indexOf(byte[], byte, int, java.lang.String, int); public int lastIndexOf(java.lang.String); public int lastIndexOf(java.lang.String, int); static int lastIndexOf(byte[], byte, int, java.lang.String, int); public java.lang.String substring(int); public java.lang.String substring(int, int); public java.lang.CharSequence subSequence(int, int); public java.lang.String concat(java.lang.String); public java.lang.String replace(char, char); public boolean matches(java.lang.String); public boolean contains(java.lang.CharSequence); public java.lang.String replaceFirst(java.lang.String, java.lang.String); public java.lang.String replaceAll(java.lang.String, java.lang.String); public java.lang.String replace(java.lang.CharSequence, java.lang.CharSequence); public java.lang.String[] split(java.lang.String, int); public java.lang.String[] split(java.lang.String); public static java.lang.String join(java.lang.CharSequence, java.lang.CharSequence...); public static java.lang.String join(java.lang.CharSequence, java.lang.Iterable<? extends java.lang.CharSequence>); public java.lang.String toLowerCase(java.util.Locale); public java.lang.String toLowerCase(); public java.lang.String toUpperCase(java.util.Locale); public java.lang.String toUpperCase(); public java.lang.String trim(); public java.lang.String strip(); public java.lang.String stripLeading(); public java.lang.String stripTrailing(); public boolean isBlank(); public java.util.stream.Stream<java.lang.String> lines(); public java.lang.String toString(); public java.util.stream.IntStream chars(); public java.util.stream.IntStream codePoints(); public char[] toCharArray(); public static java.lang.String format(java.lang.String, java.lang.Object...); public static java.lang.String format(java.util.Locale, java.lang.String, java.lang.Object...); public static java.lang.String valueOf(java.lang.Object); public static java.lang.String valueOf(char[]); public static java.lang.String valueOf(char[], int, int); public static java.lang.String copyValueOf(char[], int, int); public static java.lang.String copyValueOf(char[]); public static java.lang.String valueOf(boolean); public static java.lang.String valueOf(char); public static java.lang.String valueOf(int); public static java.lang.String valueOf(long); public static java.lang.String valueOf(float); public static java.lang.String valueOf(double); public native java.lang.String intern(); public java.lang.String repeat(int); void getBytes(byte[], int, byte); java.lang.String(char[], int, int, java.lang.Void); java.lang.String(java.lang.AbstractStringBuilder, java.lang.Void); java.lang.String(byte[], byte); byte coder(); byte[] value(); static void checkIndex(int, int); static void checkOffset(int, int); static void checkBoundsOffCount(int, int, int); static void checkBoundsBeginEnd(int, int, int); static java.lang.String valueOfCodePoint(int); public int compareTo(java.lang.Object); static {}; }
Command :$ javap java.util.Scanner
Compiled from "Scanner.java" public final class java.util.Scanner implements java.util.Iterator<java.lang.String>, java.io.Closeable { int modCount; static final boolean $assertionsDisabled; public java.util.Scanner(java.lang.Readable); public java.util.Scanner(java.io.InputStream); public java.util.Scanner(java.io.InputStream, java.lang.String); public java.util.Scanner(java.io.InputStream, java.nio.charset.Charset); public java.util.Scanner(java.io.File) throws java.io.FileNotFoundException; public java.util.Scanner(java.io.File, java.lang.String) throws java.io.FileNotFoundException; public java.util.Scanner(java.io.File, java.nio.charset.Charset) throws java.io.IOException; public java.util.Scanner(java.nio.file.Path) throws java.io.IOException; public java.util.Scanner(java.nio.file.Path, java.lang.String) throws java.io.IOException; public java.util.Scanner(java.nio.file.Path, java.nio.charset.Charset) throws java.io.IOException; public java.util.Scanner(java.lang.String); public java.util.Scanner(java.nio.channels.ReadableByteChannel); public java.util.Scanner(java.nio.channels.ReadableByteChannel, java.lang.String); public java.util.Scanner(java.nio.channels.ReadableByteChannel, java.nio.charset.Charset); public void close(); public java.io.IOException ioException(); public java.util.regex.Pattern delimiter(); public java.util.Scanner useDelimiter(java.util.regex.Pattern); public java.util.Scanner useDelimiter(java.lang.String); public java.util.Locale locale(); public java.util.Scanner useLocale(java.util.Locale); public int radix(); public java.util.Scanner useRadix(int); public java.util.regex.MatchResult match(); public java.lang.String toString(); public boolean hasNext(); public java.lang.String next(); public void remove(); public boolean hasNext(java.lang.String); public java.lang.String next(java.lang.String); public boolean hasNext(java.util.regex.Pattern); public java.lang.String next(java.util.regex.Pattern); public boolean hasNextLine(); public java.lang.String nextLine(); public java.lang.String findInLine(java.lang.String); public java.lang.String findInLine(java.util.regex.Pattern); public java.lang.String findWithinHorizon(java.lang.String, int); public java.lang.String findWithinHorizon(java.util.regex.Pattern, int); public java.util.Scanner skip(java.util.regex.Pattern); public java.util.Scanner skip(java.lang.String); public boolean hasNextBoolean(); public boolean nextBoolean(); public boolean hasNextByte(); public boolean hasNextByte(int); public byte nextByte(); public byte nextByte(int); public boolean hasNextShort(); public boolean hasNextShort(int); public short nextShort(); public short nextShort(int); public boolean hasNextInt(); public boolean hasNextInt(int); public int nextInt(); public int nextInt(int); public boolean hasNextLong(); public boolean hasNextLong(int); public long nextLong(); public long nextLong(int); public boolean hasNextFloat(); public float nextFloat(); public boolean hasNextDouble(); public double nextDouble(); public boolean hasNextBigInteger(); public boolean hasNextBigInteger(int); public java.math.BigInteger nextBigInteger(); public java.math.BigInteger nextBigInteger(int); public boolean hasNextBigDecimal(); public java.math.BigDecimal nextBigDecimal(); public java.util.Scanner reset(); public java.util.stream.Stream<java.lang.String> tokens(); public java.util.stream.Stream<java.util.regex.MatchResult> findAll(java.util.regex.Pattern); public java.util.stream.Stream<java.util.regex.MatchResult> findAll(java.lang.String); public java.lang.Object next(); static {}; }
COMMAND : javap -c Myclass
Output:
Compiled from "Myclass.java" public class Myclass { int num; public Myclass(); Code: 0: aload_0 1: invokespecial #1 // Method java/lang/Object."<init>":()V 4: aload_0 5: iconst_0 6: putfield #2 // Field num:I 9: return public Myclass(int); Code: 0: aload_0 1: invokespecial #1 // Method java/lang/Object."<init>":()V 4: aload_0 5: iload_1 6: putfield #2 // Field num:I 9: return public static void main(java.lang.String[]); Code: 0: new #3 // class Myclass 3: dup 4: invokespecial #4 // Method "<init>":()V 7: astore_1 8: aload_0 9: arraylength 10: ifle 52 13: aload_0 14: iconst_0 15: aaload 16: invokestatic #5 // Method java/lang/Integer.parseInt:(Ljava/lang/String;)I 19: istore_2 20: new #3 // class Myclass 23: dup 24: iload_2 25: invokespecial #6 // Method "<init>":(I)V 28: astore_3 29: getstatic #7 // Field java/lang/System.out:Ljava/io/PrintStream; 32: aload_1 33: getfield #2 // Field num:I 36: invokevirtual #8 // Method java/io/PrintStream.println:(I)V 39: getstatic #7 // Field java/lang/System.out:Ljava/io/PrintStream; 42: aload_3 43: getfield #2 // Field num:I 46: invokevirtual #8 // Method java/io/PrintStream.println:(I)V 49: goto 60 52: getstatic #7 // Field java/lang/System.out:Ljava/io/PrintStream; 55: ldc #9 // String Insufficient argument 57: invokevirtual #10 // Method java/io/PrintStream.println:(Ljava/lang/String;)V 60: return }
b) Write a program to calculate perimeter and area of rectangle.
(hint : area = length * breadth , perimeter=2*(length+breadth))
import java.util.Scanner; public class area { public static void main(String[] args) { int l, b, perimeter, area; Scanner s = new Scanner(System.in); System.out.print("Enter length of rectangle:"); l = s.nextInt(); System.out.print("Enter breadth of rectangle:"); b = s.nextInt(); perimeter = 2 * (l + b); System.out.println("Perimeter of rectangle:"+perimeter); area = l * b; System.out.println("Area of rectangle:"+area); } }
OUTPUT :
Enter length of rectangle:4 Enter breadth of rectangle:8 Perimeter of rectangle:24 Area of rectangle:32
c) Write a menu driven program to perform the following operations
i. Calculate the volume of cylinder. (hint : Volume: π × r2 × h)
ii. Find the factorial of given number.
iii. Check the number is Armstrong or not.
iv. Exit
import java.util.*; class menudrive { public static void main(String args[]) { Scanner s= new Scanner(System.in); System.out.println("\n1.Calculate the volume of cylinder"); System.out.println("2.Check the number is Armstrong or not."); System.out.println("3.Find Factorial"); System.out.println("4.Exit") System.out.print("\nEnter your choice :"); int choice=s.nextInt(); switch(choice) { case 1: System.out.println("Enter the radius:"); double r=s.nextDouble(); System.out.println("Enter the height:"); double h=s.nextDouble(); double volume=((22*r*r*h)/7); System.out.println("volume of Cylinder is: " +volume); break; case 2: System.out.println("Enter number :"); int number=s.nextInt(); int originalNumber, remainder, result = 0; originalNumber = number; while (originalNumber != 0) { remainder = originalNumber % 10; result += Math.pow(remainder, 3); originalNumber /= 10; } if(result == number) System.out.println(number + " is an Armstrong number."); else System.out.println(number + " is not an Armstrong number."); break; case 3: System.out.println("Enter number :"); int num=s.nextInt(); long factorial = 1; for(int i = 1; i <= num; ++i) { // factorial = factorial * i; factorial *= i; } System.out.printf("Factorial of %d = %d", num, factorial); break; case 4: return 0; default :System.out.println("Invalid input"); }
Output:
1.Calculate the volume of cylinder 2.Check the number is Armstrong or not. 3.Find Factorial 4.Exit Enter your choice :1 Enter the radius: 5 Enter the height: 8 volume of Cylinder is: 628.5714285714286 ---------------------------------------------------------------------------------------------------------------------- 1.Calculate the volume of cylinder 2.Check the number is Armstrong or not. 3.Find Factorial 4.Exit Enter your choice :2 Enter number : 369 369 is not an Armstrong number. ----------------------------------------------------------------------------------------------------------------------- 1.Calculate the volume of cylinder 2.Check the number is Armstrong or not. 3.Find Factorial 4.Exit Enter your choice :3 Enter number : 5 Factorial of 5 = 120
import java.util.*; public class ReverseArray { public static void main(String[] args) { //Initialize array int [] arr = new int [5]; Scanner sc=new Scanner(String[] args); System.out.println("Enter five numbers : "); for (int i = 0; i < arr.length; i++) { arr[i]=sc.nextInt(); } System.out.println("Original array: "); for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); } System.out.println(); System.out.println("Array in reverse order: "); //Loop through the array in reverse order for (int i = arr.length-1; i >= 0; i--) { System.out.print(arr[i] + " "); } } }
Enter number of elements in the array:5 Enter 5 elements 3 4 5 1 6 Reverse of an array is : 6 1 5 4 3
0 Comments
Thanks,To visit this blog.