Test-1 Solutions
Test-1 Solutions
Draw the class diagram for the following and Develop a java program that reads the
number of kilograms of type double, through command line arguments, converts it to
pounds, displays the result. (HINT : 1 kg =2.2046)
package question2;
import java.util.Scanner;
public class Demo {
public void convert(double a)
{
double k=a*2.2046;
System.out.println("After converting the wright into pounds the result is"+k);
}
public static void main(String[] args) {
Demo obj=new Demo();
Scanner sc=new Scanner(System.in);
System.out.println("enteer the amount of weight you need to converrt into
pounds");
double p=sc.nextDouble();
obj.convert(p);
}
}
3. Modularize to class level the following task and draw the class diagram. Develop a static
method distance () in class Geometry which accepts xl, yl, x2 and y2
choic representing the coordinates of two points of type double and then returns the
|Q-4 distance between them and call the method from main () in Demo class. Assume 4
inputs from command line. (Hint: Distance = ((x2-x1)^2 -- (y2-yl)^2y^1/2)
package Question3;
class Geometry
{
public double distance(double x1,double x2,double y1,double y2)
{
double k=(x2-x1)(x2-x1)+(y2-y1)(y2-y1);
return (Math.sqrt(k));
}
}
5. Develop a class Utility with three overloaded static methods with the method name
findSmallest (). The first method will take two arguments and find the smallest among them
ad return that value. The second method will take three arguments and
choice find smallest among them. The third method will accept an array and find the
smallest value in the array and return it. Call these three overloaded methods and test their
functionality from the main () of the Demo class which belong to another package.
package question5$2;
import java.util.Scanner;
import question5$1.*;
public class Demo {
public static void main(String [] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("enter the elements to find the smallest among the 2
elements");
int p=Utility.findSmallest(sc.nextInt(),sc.nextInt());
System.out.println("enter the elements to find the smallest among the 2
elements");
int q=Utility.findSmallest(sc.nextInt(),sc.nextInt(),sc.nextInt());
int s;
System.out.println("enter the size of the array");
s=sc.nextInt();
System.out.println("enter the elements to find the smallest amonng the
element of the array");
int k[]=new int[s];
for(int i=0;i<k.length;i++)
k[i]=sc.nextInt();
Utility.findSmallest(k);
System.out.println("The Smallest emlemeent among the elements the given
array is"+k);
package question5$1;
public class Utility {
{
if(a>b)
return b;
else
return a;
}
public static int findSmallest(int a,int b,int c)
{
if(a<b&&a<c)
return a;
else if(c<b&&c<b)
return c;
else
return b;
}
public static int findSmallest(int []a)
{
int min=a[0];
for(int i=1;i<a.length;i++)
{
if(a[i]<min)
min=a[i];
}
return min;
}
}
6. Some websites impose certain rules for passwords. Suppose the password rules are as
follows:
a. A password must have at least eight characters.
b. A password cannot have space and must have at least 1 alphabet and digit. Modularize
the design to class & package levels and draw the class diagrams. Write main () method
accepts password through command line arguments and a static method isValidPassword()
that expects a string argument and returns boolean type after validating the password.
package pack02;
class RulesForPassword
{
public static boolean isValidPassword(String s)
{
if((s.length()==8)&&(s.matches("[0-9a-zA-Z]*")) )
{
return true;
}
else if(s.equals(" "))
{
return false;
}
return false;
7. Develop class diagram and logic as per the following specifications: Develop a class
Employee with ID, firstName, MiddleName and lastName as private attributes. Code
choice the method hasSameName(Employee e): boolean that returns true if all firstName,
MiddleName and lastName match, otherwise return false. Develop main method that
creates an object and test the functionality of the above class.
package Question7;
import java.util.Scanner;
public class Employee {
String firstName;
String MiddleName;
String lastName;
int ID;
public boolean hasSameName(Employee e)
{
if(e.firstName.equals(e.lastName)&&e.firstName.equals(e.MiddleName)&&e.MiddleName.e
quals(e.lastName))
return true;
return false;
}
public static void main(String [] args)
{
Scanner sc=new Scanner(System.in);
Employee obj=new Employee();
obj.ID=sc.nextInt();
obj.firstName=sc.next();
obj.MiddleName=sc.next();
obj.lastName=sc.next();
if(obj.hasSameName(obj))
System.out.println("All the names are same");
else
System.out.println("All the names are not the same");
}
8. Draw class diagram for the following and develop the logic Define a class Complex with
real and img as its private attributes. Code a constructor that expects object of type
Complex as argument and copies the values to the invoking instance.
package question8;
import java.util.Scanner;
public class Complex
{
private double real;
private double imaginary;
static Complex obj=new Complex();
Complex()
{
}
Complex(Complex e)
{
obj=e;
}
public static void main(String [] args)
{
Scanner sc=new Scanner(System.in);
Complex obj1=new Complex();
obj1.real=sc.nextDouble();
obj1.imaginary=sc.nextDouble();
Complex obj2=new Complex(obj1);
}
}
9. Draw the class diagram modularized to package level and develop a class “Dictionary”
that contains the following information. (a) Name of a person (b)choice Mobile Number
(both as private attributes) The main () method of Demo class must be able to store details
of 10 persons and display a menu with the following options. Y 1. Add new person
information 2. Display all data.
package pack000001;
import java.util.Scanner;
public class Demo
{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
Dictionary d=new Dictionary();
while(true)
{
System.out.println("1. Add new person information \n"
+ "2. Display all data \n"
+ "3. Exit");
switch(sc.nextInt())
{
case 1:System.out.println("Enter name, number : ");
d.addNewStudent(sc.next(),sc.nextInt());
break;
case 2: d.printAllStudents();
break;
case 3: System.exit(0);
package pack000001;
10. let s1 and s2 be an ArrayList in main() of the class Utility which stores a list of integers.
develop the methods for following tasks a) search for an element in ArrayList s1 b) store all
the elements of sl into s2 c) find the smallest value in s2 ArrayList.
package Question10;
import java.util.*;
public class Utility {
static ArrayList<Integer>s1=new ArrayList<>();
static ArrayList<Integer>s2=new ArrayList<>();
public void search(int a)
{
int flag=1;
for(int i=0;i<s1.size();i++)
if(s1.get(i)==a)
flag=1;
else
flag =0;
if(flag==1)
System.out.println("the given element is present");
else
System.out.println("the given element is not present");
}
public void store()
{
for(int i=0;i<s1.size();i++)
s2.add(s1.get(i));
}
public void smallest()
{
Collections.sort(s1);
System.out.println("The smallest element in the list 1 is"+s1.get(0));
}
public static void main(String [] args) {
Utility obj=new Utility();
Scanner sc=new Scanner(System.in);
System.out.println("Enter the size of thee first list 1");
int size1=sc.nextInt();
System.out.println("Enter the elemnts of the list 1");
for(int i=0;i<size1;i++)
s1.add(sc.nextInt());
System.out.println("Enter the size of thee first list 2");
int size2=sc.nextInt();
System.out.println("Enter the elemnts of the list 2");
for(int i=0;i<size2;i++)
s2.add(sc.nextInt());
System.out.println("Enter the element that is neededto be found");
obj.search(sc.nextInt());
obj.store();
obj.smallest();
}
}
11. Modularize the design to package level and develop the code. A vehicle registral portal
accepts the following data from Vehicle owners: a) Vehicle Number (either 2 or 4) c) Owner
name d) Mobile. The Vehicle class contains parameterized constructor, toString() methods.
The VehicalDemoclass has a main VehicalDemoclass has a main () method which 10-12
Choice me reads and can stores data upto 100 vehicles into an Array and displays the
an Array and displays the menu with following operations: 1) Add data 2) Display data based
on vehicle number.
package p1;
import java.util.Scanner;
public class Vehicle {
Vehicle(){
Vn=123;
Vw=2;
Oname=" Mercedes ";
Oph=123456789L;
}
public String toString()
{
return Vn+Vw+Oname+Oph;
}
public static void addVeichal()
{
Scanner s=new Scanner(System.in);
Vn=s.nextInt();
Vw=s.nextInt();
Oname=s.next();
Oph=s.nextLong();
}
public static int searchVeichal(int n)
{
for(int i=0;i<V.length();i++)
{
if(V.getVn(i)==n)
{
return i;
}
else
{
System.out.print("Vehical number not found in ther data");
return -1;
}
}
}
}
12. Draw the class diagram and develop a class Point2D with private attributes type double
and a class Point3D which inherits Point2D and has a priva Write constructors, setters,
getters and toString() methods in both clase 10 method of class Demo to create object of
Point3D to print all values of
with private attributes x and y of in 2D and has a private attribute z. methods in both classes
and a main
to print all values of x, y and z.
package Question12;
import java.util.Scanner;
class Point2D
{
private double x;
private double y;
Point2D(double x,double y){
setX(x);
setY(y);
}
public void setX(double x) {
this.x = x;
}
public void setY(double y) {
this.y = y;
}
public double getX() {
return this.x;
}
public double getY() {
return this.y;
}
public String toString()
{
return String.format("X attributes are:%s%n Y attributes are :%s
%n",getX(),getY());
}
}
class Point3D extends Point2D
{
private double z;
Point3D(double z,double x,double y)
{
super(x,y);
setZ(z);
}
public void setZ(double z) {
this.z = z;
}