Javaese Ansbook
Javaese Ansbook
import java.util.Scanner;
if (a > b) {
if (a > c) {
System.out.println("Greatest number is: " + a);
} else {
System.out.println("Greatest number is: " + c);
}
} else {
if (b > c) {
System.out.println("Greatest number is: " + b);
} else {
System.out.println("Greatest number is: " + c);
}
}
}
}
2. Write a program to check if user entered password contains Uppercase, Lowercase, and no. Characters
using logical operators.
import java.util.Scanner;
switch (grade) {
case 'A':
System.out.println("Excellent!");
break;
case 'B':
System.out.println("Good!");
break;
case 'C':
System.out.println("Fair!");
break;
case 'D':
System.out.println("Needs Improvement!");
break;
default:
System.out.println("Invalid Grade!");
}
}
}
5. Write a program to display the following star pattern using for loop.
*
**
***
****
7. Write a program to implicitly typecast lower range data type to larger storage size data type.
public class TypeCasting {
public static void main(String[] args) {
int numInt = 100;
long numLong = numInt;
float numFloat = numLong;
8. Write a program to convert variable of basic data types and shows result of explicit typecasting.
public class TypeCastingExplicit {
public static void main(String[] args) {
double numDouble = 10.5;
int numInt = (int) numDouble;
long numLong = (long) numDouble;
// Default constructor
public ConstructorOverloading() {
this.value = 0;
}
// Parameterized constructor
public ConstructorOverloading(int value) {
this.value = value;
}
10. Write a program to show the use any 6 methods of string class.
public class StringMethods {
public static void main(String[] args) {
String str = "Hello, World!";
14. Write a program to insert different elements in the vector and display it.
import java.util.Vector;
double calculateArea() {
return length * width;
}
}
double calculateVolume() {
return length * width * height;
}
}
interface B {
void methodB();
}
20. Define a package named ‘College’ include class named as ‘Dept’ with one method to display the name
of the department. Develop a program to import this package in a java application and call the method
defined in the package.
// College/Dept.java
package College;
evenThread.start();
oddThread.start();
}
}
// Setting priorities
t1.setPriority(Thread.MIN_PRIORITY);
t2.setPriority(Thread.MAX_PRIORITY);
t1.start();
t2.start();
}
}
try {
int result = numerator / denominator;
System.out.println("Result: " + result);
} catch (ArithmeticException e) {
System.out.println("Error: " + e.getMessage());
}
}
}
24. Define a Exception called “NotMatchException” that is thrown when a string is not equal to “msbte”.
Write a program that uses this exception.
import java.io.*;
class NoMatchException extends Exception
{
NoMatchException(String s)
{
super(s);
}
}
class test1
{
public static void main(String args[]) throws IOException
{
BufferedReader br= new BufferedReader(new InputStreamReader(System.in) );
System.out.println("Enter a word:");
String str= br.readLine();
try
{
if (str.compareTo("MSBTE")!=0) // can be done with equals()
throw new NoMatchException("Strings are not equal");
else
System.out.println("Strings are equal");
}
catch(NoMatchException e)
{
System.out.println(e.getMessage());
}
}
}
if (password.length() > 8) {
System.out.println("Password length is greater than 8 characters.");
} else {
System.out.println("Password length is not greater than 8 characters.");
}
}