(maintained by Lakshmi Sarvani Videla, Former Assistant Professor, KLEF and now Computer Science Lecturer, SRR and CVR Government Degree College, Vijayawada
Thursday, 27 February 2020
Friday, 14 February 2020
Validation whether a string has only lower and upper case alphabets using regular expressions in java
String st="abcd#adl#lavjkllads";
st.matches("[a-z]*[A-Z]*");
but this will not take spaces into consideration
st.matches("[a-z]*[A-Z]*");
but this will not take spaces into consideration
SO WE USE st.matches("[a-z A-Z]*");
Example Program
Develop a program to create a class "Dictionary" that contains the following information a)Name of a person b)Mobile number. The setter method raise an exceptions when name has any other characters apart from lower and upper case alphabets and when mobile number is negative or does not have 10 digits. The parameterized constructor calls the setter and throws the Exceptions. The main() method of DictionaryDemo must handle the exceptions.
package parent;
import java.util.Scanner;
class InvalidName extends RuntimeException
{
InvalidName(String st)
{
super(st);
}
}
class InvalidMobile extends RuntimeException
{
InvalidMobile(String st)
{
super(st);
}
}
class Dictionary
{
private String name;
private long mobile;
Dictionary(String name, long mobile)
{
if(setName(name) == false)
throw new InvalidName("Not a valid name");
if(setMobile(mobile) == false)
throw new InvalidMobile("Not a valid mobile number");
}
public boolean setName(String name)
{
if(name.matches("[a-z A-Z]*"))
{
this.name = name;
return true;
}
return false;
}
public boolean setMobile(long mobile)
{
if(mobile >=1000000000L && mobile <= 9999999999L)
{
this.mobile=mobile;
return true;
}
return false;
}
}
public class DictionaryDemo
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("enter name and mobile number");
try
{
Dictionary ob = new Dictionary(sc.next(),sc.nextLong());
}
catch(InvalidName e)
{
e.printStackTrace();
}
catch(InvalidMobile e)
{
e.printStackTrace();
}
}
}
Example Program
Develop a program to create a class "Dictionary" that contains the following information a)Name of a person b)Mobile number. The setter method raise an exceptions when name has any other characters apart from lower and upper case alphabets and when mobile number is negative or does not have 10 digits. The parameterized constructor calls the setter and throws the Exceptions. The main() method of DictionaryDemo must handle the exceptions.
package parent;
import java.util.Scanner;
class InvalidName extends RuntimeException
{
InvalidName(String st)
{
super(st);
}
}
class InvalidMobile extends RuntimeException
{
InvalidMobile(String st)
{
super(st);
}
}
class Dictionary
{
private String name;
private long mobile;
Dictionary(String name, long mobile)
{
if(setName(name) == false)
throw new InvalidName("Not a valid name");
if(setMobile(mobile) == false)
throw new InvalidMobile("Not a valid mobile number");
}
public boolean setName(String name)
{
if(name.matches("[a-z A-Z]*"))
{
this.name = name;
return true;
}
return false;
}
public boolean setMobile(long mobile)
{
if(mobile >=1000000000L && mobile <= 9999999999L)
{
this.mobile=mobile;
return true;
}
return false;
}
}
public class DictionaryDemo
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("enter name and mobile number");
try
{
Dictionary ob = new Dictionary(sc.next(),sc.nextLong());
}
catch(InvalidName e)
{
e.printStackTrace();
}
catch(InvalidMobile e)
{
e.printStackTrace();
}
}
}
Thursday, 6 February 2020
Constructor Chaining in java
public class Chaining {
private double l,b,h;
public Chaining()
{
l=b=h=10;
}
public Chaining(double le, double br, double he)
{
this();
setDimensions(le,br,he);
}
public boolean setDimensions(double le, double br, double he)
{
if(le >= 0 && br>=0 && he>=0)
{
l=le;
b=br;
h=he;
return true;
}
return false;
}
public String toString()
{
String output="Length = "+l+" Breadth = "+b+" Height = "+h;
return output;
}
public static void main(String args[])
{
Chaining ob = new Chaining(-10,2,3);
System.out.println(ob);
}
}
private double l,b,h;
public Chaining()
{
l=b=h=10;
}
public Chaining(double le, double br, double he)
{
this();
setDimensions(le,br,he);
}
public boolean setDimensions(double le, double br, double he)
{
if(le >= 0 && br>=0 && he>=0)
{
l=le;
b=br;
h=he;
return true;
}
return false;
}
public String toString()
{
String output="Length = "+l+" Breadth = "+b+" Height = "+h;
return output;
}
public static void main(String args[])
{
Chaining ob = new Chaining(-10,2,3);
System.out.println(ob);
}
}
Friday, 10 January 2020
Simple Inheritance, Multilevel inheritance and Hierarchical inheritance in java
Q) Simple Inheritance
Explain the above using GeometricShape Class with attributes borderColor(String), filled (Boolean type). This is inherited by Rectangle Class with length and width as attributes. Add mutators, accessors and toString() methods
Multilevel Inheritance
Enhance the above design where Cuboid class with height field inherits the Rectangle class.
Hierarchy Inheritance
Enhance the design of Simple Inheritance where Circle class with radius field also inherits from GeometricShape.
class GeometricShapeClass
{
private String bColor;
private boolean filled;
public GeometricShapeClass(String c, boolean f)
{
bColor=c;
filled=f;
}
public String toString()
{
return " Border color = "+bColor+" filled = "+filled;
}
}
class Rectangle extends GeometricShapeClass
{
double l,w;
public Rectangle(String c, boolean f,double le, double wi)
{
super(c,f);
l=le;
w=wi;
}
public String toString()
{
return super.toString()+ " length = " +l+ " width = " + w;
}
}
class Cuboid extends Rectangle
{
double h;
public Cuboid(String c, boolean f,double le, double wi, double he)
{
super(c,f,le,wi);
h=he;
}
public String toString()
{
return super.toString()+" height = "+h;
}
}
class Circle extends GeometricShapeClass
{
double radius;
public Circle(String c, boolean f, double r)
{
super(c,f);
radius=r;
}
public String toString()
{
return super.toString()+ " radius = "+radius;
}
}
public class Demo
{
public static void main(String args[])
{
Cuboid ob1 = new Cuboid("black",true,10,30,60);
System.out.println(ob1);
Circle ob2 = new Circle("red",false,20.5);
System.out.println(ob2);
}
}
Explain the above using GeometricShape Class with attributes borderColor(String), filled (Boolean type). This is inherited by Rectangle Class with length and width as attributes. Add mutators, accessors and toString() methods
Multilevel Inheritance
Enhance the above design where Cuboid class with height field inherits the Rectangle class.
Hierarchy Inheritance
Enhance the design of Simple Inheritance where Circle class with radius field also inherits from GeometricShape.
class GeometricShapeClass
{
private String bColor;
private boolean filled;
public GeometricShapeClass(String c, boolean f)
{
bColor=c;
filled=f;
}
public String toString()
{
return " Border color = "+bColor+" filled = "+filled;
}
}
class Rectangle extends GeometricShapeClass
{
double l,w;
public Rectangle(String c, boolean f,double le, double wi)
{
super(c,f);
l=le;
w=wi;
}
public String toString()
{
return super.toString()+ " length = " +l+ " width = " + w;
}
}
class Cuboid extends Rectangle
{
double h;
public Cuboid(String c, boolean f,double le, double wi, double he)
{
super(c,f,le,wi);
h=he;
}
public String toString()
{
return super.toString()+" height = "+h;
}
}
class Circle extends GeometricShapeClass
{
double radius;
public Circle(String c, boolean f, double r)
{
super(c,f);
radius=r;
}
public String toString()
{
return super.toString()+ " radius = "+radius;
}
}
public class Demo
{
public static void main(String args[])
{
Cuboid ob1 = new Cuboid("black",true,10,30,60);
System.out.println(ob1);
Circle ob2 = new Circle("red",false,20.5);
System.out.println(ob2);
}
}
Menu Driven Java Program for Voter Management System
Q11
The election commission requires the following task to be done. A class
Voter with ID, name, age and address as fields. Write toString(), getter methods and setData() method. The VoterDemo has main() method which reads and stores data of 10 voters and displays a menu with following operations:
a) Add new voter
b) Search voter based on ID
c) Search voter whose name starts with “A”
solution:
package p2;
public class Voter
{
private long id;
private String name,address;
private int age;
public void setData(long id, String name,int age,String address)
{
this.id=id;
this.name=name;
this.age=age;
this.address=address;
}
public String toString()
{
return "Voter id = "+id+" Voter Name =" +name+" Voter age =" +age+"\n Address = "+address;
}
public long getId()
{
return id;
}
public String getName()
{
return name;
}
}
package p2;
import java.util.Scanner;
public class VoterDemo
{
public static int linearSearch(Voter v[],int n,long key)
{
for(int i=0;i<n;i++)
{
if(v[i].getId()==key)
return i;
}
return -1;
}
public static void linearSearch(Voter v[],int n)
{
int flag=0;
for(int i=0;i<n;i++)
{
if(v[i].getName().charAt(0) == 'A')
{
flag=1;
System.out.println(v[i]);
}
}
if(flag ==0)
System.out.println("no voter whose name starts with A");
}
public static void main(String[] args)
{
Voter v[]=new Voter[10];
Scanner sc = new Scanner(System.in);
int choice,nov=0,index;
while(true)
{
System.out.println("Menu");
System.out.println("1. Add new voter");
System.out.println("2. Search voter based on ID");
System.out.println("3. Search voter whose name starts with 'A'");
System.out.println("4. exit");
choice =sc.nextInt();
switch(choice)
{
case 1:
v[nov]=new Voter();
System.out.println("Enter id, name, age and address of voter");
long id=sc.nextLong();
sc.nextLine();
String name=sc.nextLine();
int age=sc.nextInt();
sc.nextLine();
String address=sc.nextLine();
v[nov].setData(id,name,age,address);
nov++;
break;
case 2:
System.out.println("enter the id of voter");
long key=sc.nextLong();
index=linearSearch(v,nov,key);
if(index != -1)
{
System.out.println("voter details are");
System.out.println(v[index]);
}
else
System.out.println("Search id not found");
break;
case 3:
linearSearch(v,nov);
break;
case 4:
System.exit(0);
}
}
}
}
Subscribe to:
Posts (Atom)