Professional Documents
Culture Documents
Shivam Milestone1 PDF
Shivam Milestone1 PDF
ROLL NO-96
IT- (B)
JAVA FUNDAMENTALS
Language Basics Assignment
1. Write a Program that accepts two Strings as command line
arguments and generate the output in a specific way as given
below.
Example:
If the command line arguments are ABC and Mumbai then the
output generated should be ABC Technologies Mumbai
SOLUTION
package Milestone1.javafundamentals.Basics;
import java.util.*;
public class Assignment1 {
}
2.Write a Program to accept two integers through the
command line argument and print the sum of the two numbers
SOLUTION
import java.util.Scanner;
public class Assignment2 {
}
FLOW CONTROL ASSIGNMENT
1.Write a program to check if a given number is Positive,
Negative, or Zero.
SOLUTION
import java.util.Scanner;
if (x < 0) System.out.println("Negative");
else if (x == 0) System.out.println("Zero");
else System.out.println("Positive");
main(args);
sc.close();
}
}
2.Write a program to check if a given number is odd or even.
SOLUTION
import java.util.Scanner;
if (x % 2 == 0) System.out.println("Even");
else System.out.println("Odd");
main(args);
sc.close();
}
}
3.Initialize two character variables in a program and display the
characters in alphabetical order. Eg1) if first character is s and
second is e O/P: e,s Eg2) if first character is a and second is e
O/P: a,e.
SOLUTION
public class Assignment4 {
if (x < y) {
System.out.println(x + ", " + y);
} else {
System.out.println(y + ", " + x);
}
}
4.Write a program to accept gender ("Male" or "Female") and
age (1-120) from command line arguments and print the
percentage of interest based on the given conditions. Interest
== 8.2% Gender ==> Female Age ==>1 to 58 Interest == 7.6%
Gender ==> Female Age ==>59 -120 Interest == 9.2% Gender
==> Male Age ==>1-60 Interest == 8.3% Gender ==> Male
Age ==>61-120.
SOLUTION
import java.util.*;
public class Assignment6 {
SOLUTION
public class Assignment7 {
if (Character.isLowerCase(ch))
System.out.println(ch + "->" +
Character.toUpperCase(ch));
else
System.out.println(ch + "->" +
Character.toLowerCase(ch));
SOLUTION
import java.util.*;
import java.time.Month;
public class Assignment9 {
System.out.println(monthStr);
SOLUTION
public class Assignment12 {
if (isPrime) System.out.println("prime");
else System.out.println("not prime");
}
SOLUTION
public class Assignment13 {
return isPrime;
}
SOLUTION
package Milestone1.javafundamentals.Flowcontrol;
import java.util.*;
public class Assignment17 {
int ip = sc.nextInt();
int op = 0;
int i = (int) Math.pow(10, String.valueOf(ip).length() -
1);
while (ip != 0) {
int digit = ip % 10;
op += digit * i;
i /= 10;
ip /= 10;
}
System.out.println(op);
sc.close();
}
SOLUTION
package Milestone1.javafundamentals.Flowcontrol;
import java.util.*;
public class Assignment18 {
Arrays Assignment
1.Write a program to initialize an integer array and print the
sum and average of the array.
SOLUTION
package Milestone1.javafundamentals.Arrays;
public class Assignment1 {
public static void main(String[] args) {
int[] array = {5, 4, 3, 9, 1, 7, 9};
double sum = 0;
double avg;
SOLUTION
package Milestone1.javafundamentals.Arrays;
public class Assignment2 {
System.out.println(index);
}
SOLUTION
package Milestone1.javafundamentals.Arrays;
public class Assignment4 {
public static void main(String[] args) {
int[] array = {48, 55, 68, 88, 101, 122};
SOLUTION
package Milestone1.javafundamentals.Arrays;
import java.util.Arrays;
SOLUTION
package Milestone1.javafundamentals.Arrays;
import java.util.Arrays;
Arrays.sort(array);
System.out.println(Arrays.toString(array));
}
7. Write a program to remove the duplicate elements in an
array and print .
SOLUTION
package Milestone1.javafundamentals.Arrays;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
System.out.println(Arrays.toString(distinctArray.toArray()));
}
OOPS/INHERITANCE
Object And Classes Assignment
1.Create a class Box that uses a parameterized method to
initialize the dimensions of a box.(dimensions are width, height,
depth of double type). The class should have a method that can
return volume. Obtain an object and print the corresponding
volume in main() function.
SOLUTION
package Milestone1.Oops.ObjectClasses;
class Box {
private double width;
private double height;
private double depth;
SOLUTION
package Milestone1.Oops.ObjectClasses;
class Calculator {
public static int powerInt(int num1, int num2) {
return (int) Math.pow(num1, num2);
}
}
ENCAPSULATION/ABSTRACTION ASSIGNMENT
1.Create a class called Author is designed as follows:
SOLUTION
package Milestone1.Oops.Encapsulation;
class Author {
private String name;
private String email;
private char gender;
class Book {
private String name;
private Author author;
private double price;
private int qtyInStock;
System.out.println(book);
}
}
INHERITANCE ASSIGNMENT
1.Create a class named ‘Animal’ which includes methods like
eat() and sleep(). Create a child class of Animal named ‘Bird’ and
override the parent class methods. Add a new method named
fly(). Create an instance of Animal class and invoke the eat and
sleep methods using this object.Create an instance of Bird class
and invoke the eat, sleep and fly methods using this object.
SOLUTION
package Milestone1.Oops.Inheritance;
class Animal {
SOLUTION
Person Class
package Milestone1.Oops.Inheritance;
public class Person {
protected String name;
public Person(String name) {
this.name = name;
}
@Override
public String toString() {
return "Person [name=" + name + "]";
}
Employee Class
package Milestone1.Oops.Inheritance;
public class Employee extends Person {
private double annualSalary;
private int yearOfJoining;
private String nationalInsuranceNo;
Main Class
package Milestone1.Oops.Inheritance;
}
STRING & STRING BUFFER ASSIGNMENT
1.Write a Program that will check whether a given String is
Palindrome or not.
SOLUTION
package Milestone1.Oops.String;
public class Assignment1 {
public static boolean isPalindrome (String input1) {
int digitCount = input1.length();
boolean isPalindrome = true;
return isPalindrome;
}
SOLUTION
package Milestone1.Oops.String;
public class Assignment2 {
str1 = str1.toLowerCase();
str2 = str2.toLowerCase();
if (str1.charAt(str1.length() - 1) == str2.charAt(0)) {
sb.append(str2.substring(1, str2.length()));
} else {
sb.append(str2);
}
System.out.println(sb);
sb.append(str1);
}
3.Given a string, return a new string made of n copies of the
first 2 chars of the original string where n is the length of the
string. The string may be any length. If there are fewer than 2
chars, use whatever is there. If input is "Wipro" then output
should be "WiWiWiWiWi".
SOLUTION
package Milestone1.Oops.String;
public class Assignment3 {
SOLUTION
package Milestone1.Oops.String;
public class Assignment4 {
String output;
if (str.length() % 2 == 0) {
output = str.substring(0, str.length() / 2);
} else {
output = null;
}
System.out.println(output);
}
}
5.Given a string, return a version without the first and last char,
so "Wipro" yields "ipr". The string length will be at least 2.
SOLUTION
package Milestone1.Oops.String;
public class Assignment5 {
System.out.println(str);
}
}
6.Given 2 strings, a and b, return a string of the form
short+long+short, with the shorter string on the outside and
the longer string on the inside. The strings will not be the same
length, but they may be empty (length 0). If input is "hi" and
"hello", then output will be "hihellohi".
SOLUTION
package Milestone1.Oops.String;
public class Assignment6 {
System.out.println(output);
}
}
7.Given a string, if the first or last chars are 'x', return the string
without those 'x' chars, and otherwise return the string
unchanged. If the input is "xHix", then output is "Hi".
SOLUTION
package Milestone1.Oops.String;
public class Assignment7 {
if (str.charAt(0) == 'x')
str = str.substring(1, str.length());
if (str.charAt(str.length() - 1) == 'x')
str = str.substring(0, str.length() - 1);
System.out.println(str);
}
ABSTRACTION/PACKAGES/EXCEPTION HANDLING
ABSTRACT CLASSES ASSIGNMENT
1.Create an abstract class Compartment to represent a rail
coach. Provide an abstract function notice in this class. Derive
FirstClass, Ladies, General, Luggage classes from the
compartment class. Override the notice function in each of
them to print notice suitable to the type of the compartment.
Create a class TestCompartment . Write main function to do the
following: Declare an array of Compartment of size 10. Create a
compartment of a type as decided by a randomly generated
integer in the range 1 to 4. Check the polymorphic behavior of
the notice method.
SOLUTION
Compartment Class
package Milestone1.ExceptionHandling.AbstractClass;
FirstClass
package Milestone1.ExceptionHandling.AbstractClass;
General Class
package Milestone1.ExceptionHandling.AbstractClass;
Ladies Class
package Milestone1.ExceptionHandling.AbstractClass;
Luggage Class
package Milestone1.ExceptionHandling.AbstractClass;
TestCompartment Class
package Milestone1.ExceptionHandling.AbstractClass;
import java.util.Random;
if (randomNum == 1)
compartments[i] = new FirstClass();
else if (randomNum == 2)
compartments[i] = new Ladies();
else if (randomNum == 3)
compartments[i] = new General();
else if (randomNum == 4)
compartments[i] = new Luggage();
compartments[i].notice();
}
}
}
EXCEPTION HANDLING ASSIGNMENT
1.Write a program that takes as input the size of the array and
the elements in the array. The program then asks the user to
enter a particular index and prints the element at that index.
This program may generate Array Index Out Of Bounds
Exception. Use exception handling mechanisms to handle this
exception. In the catch block, print the class name of the
exception thrown.
SOLUTION
package Milestone1.ExceptionHandling.Exception;
import java.util.Scanner;
System.out.println("java.lang.ArrayIndexOutOfBoundsException")
;
}
sc.close();
}
}
2.Write a class MathOperation which accepts integers from
command line. Create an array using these parameters. Loop
through the array and obtain the sum and average of all the
elements. Display the result. Check for various exceptions that
may arise like ArithmeticException, NumberFormatException,
and so on.
SOLUTION
package Milestone1.ExceptionHandling.Exception;
import java.util.InputMismatchException;
import java.util.*;
int sum = 0;
double avg = 0;
try {
for (int i = 0; i < n; i++)
arr[i] = scn.nextInt();
for (int i = 0; i < n; i++)
sum += arr[i];
avg = sum / n;
} catch (NumberFormatException e) {
System.out.println("NumberFormatException");
} catch (ArithmeticException e) {
System.out.println("ArithmeticException");
} catch (InputMismatchException e) {
System.out.println("InputMismatchException");
}
SOLUTION
ValuesOutOfRangeException Class
NegativeValuesException Class
Main Class
import java.util.Scanner;
try {
name = sc.nextLine();
if (sc.hasNextInt())
subA = sc.nextInt();
else
throw new NumberFormatException();
if (sc.hasNextInt())
subB = sc.nextInt();
else
throw new NumberFormatException();
if (sc.hasNextInt())
subC = sc.nextInt();
else
throw new NumberFormatException();
} catch (ArithmeticException e) {
System.out.println(e.getMessage());
} catch (NegativeValuesException e) {
System.out.println(e.getMessage());
} catch (ValuesOutOfRangeException e) {
System.out.println(e.getMessage());
}
sc.close();
}
4.Write a program to accept name and age of a person from
the command prompt(passed as arguments when you execute
the class) and ensure that the age entered is >=18 and < 60.
Display proper error messages. The program must exit
gracefully after displaying the error message in case the
arguments passed are not proper. (Hint : Create a user defined
exception class for handling errors).
SOLUTION
InvalidAgeException Class
package Milestone1.ExceptionHandling.Exception;
public class InvalidAgeException extends Exception {
public InvalidAgeException() {
super();
System.out.println("Invalid age");
}
}
Main Class
package Milestone1.ExceptionHandling.Exception;
import java.util.Scanner;
public class Assignment4 {
}
WRAPPER CLASSES ASSIGNMENT
1.Write a java program which generates the minimum and
maximum value for each of the Numeric Wrapper classes (Byte,
Short, Integer, Long, Float, Double).
SOLUTION
package Milestone1.WrapperClasses;
public class Assignment1 {
System.out.println("Double range:");
System.out.println("min: " + Double.MIN_VALUE);
System.out.println("max: " + Double.MAX_VALUE);
System.out.println("Long range:");
System.out.println("min: " + Long.MIN_VALUE);
System.out.println("max: " + Long.MAX_VALUE);
System.out.println("Short range:");
System.out.println("min: " + Short.MIN_VALUE);
System.out.println("max: " + Short.MAX_VALUE);
System.out.println("Byte range:");
System.out.println("min: " + Byte.MIN_VALUE);
System.out.println("max: " + Byte.MAX_VALUE);
System.out.println("Float range:");
System.out.println("min: " + Float.MIN_VALUE);
System.out.println("max: " + Float.MAX_VALUE);
}
SOLUTION
package Milestone1.WrapperClasses;
import java.util.Scanner;
public class Assignment2 {
SOLUTION
package Milestone1.WrapperClasses;
import java.util.Scanner;
sc.close();
SOLUTION
package Milestone1.WrapperClasses;
class Employee implements Cloneable {
private String name;
public Employee(String name) {
this.name = name;
}
empClone.setName("John Doe");
System.out.println(empClone.getName());
System.out.println(emp.getName());
}