Java Questions
Java Questions
Assessment - II
Course Name: Programming in JAVA
1. Develop a class TelephoneIndex with two String objects as members. One for should hold
people’s name and the other should hold their phone number. The class should have
appropriate constructor, input and display methods. Create array of objects for
TelephoneIndex and do the following:
a. Your program should ask the user to enter a name or the first few characters of a
name to search for it in the array.
b. The program should display all of the names that match the user’s input and their
corresponding phone numbers.
Code:
import java.util.*;
class TelephoneIndex
String name;
String phno;
name = x1;
phno = x2;
void input()
String s1 = sa.next();
String s2 = sa.next();
void display()
NAMAN PANDEY 16BCI0030
String s1 = sa.next();
String s2 = sa.next();
System.out.println("__________________________");
System.out.println("__________________________");
if( obj[i].name.equals(ser))
System.out.println("Match found");
obj[i].display();
}
NAMAN PANDEY 16BCI0030
Screenshots:
NAMAN PANDEY 16BCI0030
2.Write a program to read a chemical equation and find out the count of the reactants and
the products. Also display the count of the number of molecules of each reactant and prod
System.out.println("____________________________
____ ");
String rec = equ.substring(0,q);
System.out.println("reactants: "+ rec);
String pro = equ.substring((q+1),l_equ);
System.out.println("product: "+ pro);
int l_rec = rec.length();
int l_pro = pro.length();
int k1=1;int k2=1;
for(int i=0 ; i < l_rec ; i++)
{
if(rec.charAt(i) == '+')
{
k1++;
}
}
for(int i=0 ; i < l_pro ; i++)
{
if(pro.charAt(i) == '+')
{
k2++;
}
}
System.out.println("____________________________
____ ");
String[] r = new String[k1];
String[] p = new String[k2];
// seperating the components
int pos = 0;
int j = 0;
char temp1 = rec.charAt(0);
NAMAN PANDEY 16BCI0030
}
}
r[j] = rec.substring(pos,(l_rec));
j = 0;
pos = 0;
for(int i=0 ; i < l_pro ; i++)
{
if(pro.charAt(i) == '+')
{
p[j] = pro.substring(pos,i);
pos = (i+1);
j++;
}
}
p[j] = pro.substring(pos,(l_pro));
char x2 = p[i].charAt(0);
if(Character.isDigit(x2))
{
int ten2 =
Character.getNumericValue(x2);
rc[i] = ten2;
p1[i] = p[i].substring(1,t_len1);
}
else
{
rc[i] = 1;
p1[i] = p[i].substring(0,t_len1);
}
}
}
for(int i = 0 ; i < k2 ; i++)
{
if(i == 0)
{
System.out.print("Prodcuts are
"+rc[i]+" moles of "+p1[i]+" and ");
}
else if(i == (k1 - 1))
{
System.out.println(rc[i]+ " moles of
"+p1[i]+".");
}
else
{
NAMAN PANDEY 16BCI0030
}
}
Screenshots:
c. Create a class Time which holds hour, minute and seconds of a 24H format time
through members.
d. Design various constructors to initiate the time such as null constructor, copy
constructor and parametrized constructor.
e. Define method printTime() which prints the time in “HH:MM:SS” format.
f. Define a method to add two times and return a valid time named as
Time addTime(Time)
g. Define a method named as void timeDifference(Time), which finds the difference
between two times and print the difference.
h. Create a Demo class to create object and call the above methods. Give the
necessary outputs.
CODE:
import java.util.*;
class Time
{
int hours;
int minutes;
int seconds;
Time()
{
hours = 0;
minutes = 0;
seconds = 0;
}
NAMAN PANDEY 16BCI0030
System.out.println(this.hours+":"+this.minutes+":"+thi
s.seconds);
System.out.println("____________________________
__________");
}
String[] s1 = t_time.split("\\s+");
int y = (x3+arr1[2])/60;
x3 = (x3+arr1[2])%60;
int y1 = (y + x2 + arr1[1])/60;
x2 = (y + x2 + arr1[1])%60;
x1 = (y1 + x1 + arr1[0])%24;
int y2 = (y1 + x1 + arr1[0])/24;
if(y2 > 1)
{
System.out.println("overflow error in hour:
NAMAN PANDEY 16BCI0030
");
}
String
adTime=Integer.toString(x1)+":"+Integer.toString(x2)+":"
+Integer.toString(x3);
return adTime;
}
System.out.println("____________________________
__________");
Time obj = new Time(n1,n2,n3);
obj.printTime();
String yt_time = Integer.toString(obj.hours)+"
"+Integer.toString(obj.minutes)+"
"+Integer.toString(obj.seconds);
String lasadd = obj.addTime(yt_time);
System.out.println("after addition time is: "+
lasadd);
System.out.println("____________________________
__________");
String lassub = obj.timeDifference(yt_time);
System.out.println("after subtraction time is:
"+ lassub);
}
}
Screenshots:
NAMAN PANDEY 16BCI0030
4.Create a class Student with name, registernumber and qualifying marks as members. Declare
all the instance variables as private and define corresponding getter methods and setter
methods for accessing the instance variables (for example, the setter method for the instance
variable name should be defined as void setName(int name) and getter method should be
defined as String getName
Create another class Demo and instantiate the Student class and set the instance variables
using setter methods. Create 3 objects of student class and set the values of instance variables
and display the details of each of object if the qualifying marks is greater than 70.
a. Create a static variable count in Student class, and define a static method int
getStudentCount() in Student class. Initialize the count value with 1000 and value
of count should be incremented when an object of Student class is created. Create
3 objects of Student class and display the value of count using getStudentCount
method after each object creation.
b. Declare an array of type Student in main() function in Demo class. Use this Student
array of instantiating the student class, setting the instance variables and
displaying the output
CODE:
import java.util.*;
class Student
int getStudentCount()
return this.count;
{
NAMAN PANDEY 16BCI0030
this.name = n1;
return this.name;
this.registernumber = n1;
return this.registernumber;
this.qmarks = n1;
return this.qmarks;
class Demo
{
NAMAN PANDEY 16BCI0030
obj[0].count++;
obj[1].count++;
obj[2].count++;
System.out.println("______________________________________\n");
n1[i]
= sa.next();
n2[i]
= sa.next();
NAMAN PANDEY 16BCI0030
n3[i]
= sa.nextInt();
obj[i].setName(n1[i]);
obj[i].setRegno(n2[i]);
obj[i].setQmarks(n3[i]);
int x1 = obj[i].getQmarks();
}}
}}
SCREENSHOT:
NAMAN PANDEY 16BCI0030
5.Write a program in Java to raise exception for data validation and typo error.
a. Read the Register Number and Mobile Number of a student. If the Register
Number does not contain exactly 9 characters or if the Mobile Number does
not contain exactly 10 characters, throw an IllegalArgumentException.
b. If the Mobile Number contains any character other than a digit, raise a
NumberFormatException.
c. If the Register Number contains any character other than digits and alphabets,
throw a NoSuchEl
CODE:
import java.util.*;
class code5
{
static void len(int q1 , int q2)
{
if( q1 != 10 || q2 != 9)
{
throw new IllegalArgumentException("illegal length
of input");
}
else
{
System.out.println("The input length accurate.");
}
}
static void mob(String q1)
{
int k = 0;
for(int i = 0 ; i< 10 ; i++)
{
char y = q1.charAt(i);
if(Character.isDigit(y))
{
k++;
}
else
{
throw new
NumberFormatException("invaid format ");
}
}
if(k == 10)
{
System.out.println("The mobile
number format is correct. ");
}
NAMAN PANDEY 16BCI0030
}
static void reg(String q2)
{
int k = 0;
for(int i = 0 ; i< 9 ; i++)
{
char y = q2.charAt(i);
if(Character.isDigit(y))
{
k++;
}
else if(Character.isLetter(y))
{
k++;
}
else
{
throw new
NoSuchElementException("invaid format");
}
}
if(k == 9)
{
System.out.println("The registration
number format is correct. ");
}
}
SCREENSHOTS: