Вы находитесь на странице: 1из 18

NAMAN PANDEY 16BCI0030

Assessment - II
Course Name: Programming in JAVA

Course Code: CSE1007 Max. Marks : 10

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;

TelephoneIndex(String x1, String x2)

name = x1;

phno = x2;

void input()

Scanner sa = new Scanner(System.in);

System.out.println("enter your name: ");

String s1 = sa.next();

System.out.println("enter your phone number: ");

String s2 = sa.next();

void display()
NAMAN PANDEY 16BCI0030

System.out.println("your output is: "+this.name);

System.out.println("your output is: "+this.phno);

public static void main(String ards[])

Scanner sa = new Scanner(System.in);

TelephoneIndex obj[] = new TelephoneIndex[5]; //creating array of objects

for(int i = 0 ; i < 5 ; i++)

System.out.println("enter your name: ");

String s1 = sa.next();

System.out.println("enter your phone number: ");

String s2 = sa.next();

obj[i] = new TelephoneIndex(s1,s2); // initializing values

System.out.println("__________________________");

System.out.println("enter your name you want to search: ");

String ser = sa.next();

System.out.println("__________________________");

for(int i = 0 ; i < 5 ; i++)

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

Eg., equation, 2NaOH + H2SO4 -> Na2SO4+ 2H2


Reactants are 2 moles of NaOH and 1 mole of H2SO4.
Products are 1 mole of Na2SO4 and 2 moles of H2O.
CODE:
import java.util.*;
class Equation
{
public static void main(String ares[])
{
Scanner sa = new Scanner(System.in);
System.out.println("enter the equation");
String equ = sa.next();
int l_equ = equ.length();
int q = equ.indexOf("="); // to know the seperation in
the equation
//System.out.println("ent: "+ q);

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

for(int i=0 ; i < l_rec ; i++)


{
if(rec.charAt(i) == '+')
{
String temp = rec.substring(pos,i);
r[j] = temp;
pos = (i+1);
j++;

}
}
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));

// to store the number of moles


int[] pc = new int[k1];
int[] rc = new int[k2];
// TO STORE JUST REACTANTS WITHOUT MOLE VALUE
String[] r1 = new String[k1];
String[] p1 = new String[k2];

for(int i = 0 ; i < k1 ; i++)


{
int t_len = r[i].length();
char x1 = r[i].charAt(0);
if(Character.isDigit(x1))
{
int ten1 =
Character.getNumericValue(x1);
pc[i] = ten1;
r1[i] = r[i].substring(1,t_len);
}
else
{
pc[i] = 1;
r1[i] = r[i].substring(0,t_len);
}
}
for(int i = 0 ; i < k2 ; i++)
{
int t_len1 = p[i].length();
NAMAN PANDEY 16BCI0030

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 < k1 ; i++)


{
if(i == 0)
{
System.out.print("Reactants are
"+pc[i]+" moles of "+r1[i]+" and ");
}
else if(i == (k1 - 1))
{
System.out.println(pc[i]+ " moles of
"+r1[i]+".");
}
else
{
System.out.print(pc[i]+ " moles of
"+r1[i]+" and ");
}

}
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

System.out.print(rc[i]+ " moles of


"+p1[i]+" and ");
}
}

}
}

Screenshots:

3.Develop Java code to meet following criteria:

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

Time(int x1, int x2, int x3)


{
hours = x1;
minutes = x2;
seconds = x3;
}
Time(Time obb)
{
hours = obb.hours;
minutes = obb.minutes;
seconds = obb.seconds;
}
void printTime()
{
System.out.println("The user entered time is: ");

System.out.println(this.hours+":"+this.minutes+":"+thi
s.seconds);

System.out.println("____________________________
__________");
}

String addTime(String t_time)


{

String[] s1 = t_time.split("\\s+");

int[] arr1 = new int[3];

for(int i = 0 ; i< 3 ; i++)


{
arr1[i] = Integer.parseInt(s1[i]);
}
Scanner sa = new Scanner(System.in);
System.out.println("enter the another time for
addition in hour:minute:second :-");
int x1 = sa.nextInt();
int x2 = sa.nextInt();
int x3 = sa.nextInt();

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;
}

String timeDifference(String t_time)


{
String[] s1 = t_time.split("\\s+");

int[] arr1 = new int[3];

for(int i = 0 ; i< 3 ; i++)


{
arr1[i] = Integer.parseInt(s1[i]);
}
Scanner sa = new Scanner(System.in);
System.out.println("enter the another time for
subtration in hour:minute:second :-");
int x1 = sa.nextInt();
int x2 = sa.nextInt();
int x3 = sa.nextInt();

int tota = ((x1*60*60)+(x2*60)+x3);


int totb = ((arr1[0]*60*60)+(arr1[1]*60)+arr1[2]);
int diff;
if(tota > totb)
{
diff = tota - totb;
}
else
{
diff = totb - tota;
}
x1 = diff / 3600;
int yy = diff % 3600;
x2 = yy / 60;
x3 = yy % 60;
String
subTime=Integer.toString(x1)+":"+Integer.toString(x2)+":
"+Integer.toString(x3);
return subTime;
}
}
class demo1
{
NAMAN PANDEY 16BCI0030

public static void main(String asd[])


{
Scanner sa = new Scanner(System.in);
System.out.println("enter the time in
hour:minute:second :-");
int n1 = sa.nextInt();
int n2 = sa.nextInt();
int n3 = sa.nextInt();

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

static int count = 1000;

private String name;

private String registernumber;

private int qmarks;

int getStudentCount()

return this.count;

public void setName(String n1)

{
NAMAN PANDEY 16BCI0030

this.name = n1;

public String getName()

return this.name;

public void setRegno(String n1)

this.registernumber = n1;

public String getRegno()

return this.registernumber;

public void setQmarks(int n1)

this.qmarks = n1;

public int getQmarks()

return this.qmarks;

class Demo

{
NAMAN PANDEY 16BCI0030

public static void main(String ar[])

Scanner sa = new Scanner(System.in);

Student obj[] = new Student[3];

obj[0] = new Student();

obj[0].count++;

System.out.println("count value is after creating first object: "+obj[0].getStudentCount());

//Student obj2 = new Student();

obj[1] = new Student();

obj[1].count++;

System.out.println("count value is after creating second object:


"+obj[1].getStudentCount());

//Student obj3 = new Student();

obj[2] = new Student();

obj[2].count++;

System.out.println("count value is after creating third object:


"+obj[2].getStudentCount());

String[] n1 = new String[3];

String[] n2 = new String[3];

int[] n3 = new int[3];

System.out.println("______________________________________\n");

for(int i = 0 ; i< 3 ;i++)

System.out.println("enter the name , registration number and marks of "+(i+1)+": ");

n1[i]
= sa.next();

n2[i]
= sa.next();
NAMAN PANDEY 16BCI0030

n3[i]
= sa.nextInt();

for(int i = 0 ; i < 3 ; i++)

obj[i].setName(n1[i]);

obj[i].setRegno(n2[i]);

obj[i].setQmarks(n3[i]);

int x1 = obj[i].getQmarks();

if(x1 > 70)

System.out.println("name is: "+ obj[i].getName());

System.out.println("registration number is: "+ obj[i].getRegno());

}}

}}

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. ");
}
}

public static void main(String agrs[])


{
Scanner sa = new Scanner(System.in);
System.out.println("enter the phone number: ");
String phno = sa.next();
System.out.println("enter the registration number: ");
String regno = sa.next();
int len1 , len2;
len1 = phno.length();
len2 = regno.length();

code5 obj = new code5();


obj.len(len1 , len2);
obj.mob(phno);
obj.reg(regno);
}
}
NAMAN PANDEY 16BCI0030

SCREENSHOTS:

One without exception:

One with IllegalArgumentException:

One with NumberFormatException:


NAMAN PANDEY 16BCI0030

One with NoSuchEl:

Вам также может понравиться