Você está na página 1de 4

PMR3 - LAB@Home

3 questions:
Strings, regular expressions,
exceptions and assertions.
Generics and collections
1.Write a program that accepts the URL of the Web portal from the user, such as
http://www.xyz.com, www.xyz.com, or ftp://xyz.com, and validate the format of th
e URL with the following fields:
Protocol, such as https or http
Domain, such as google or yahoo
Top level domain, such as .co or .com
"^((https?|ftp)://|(www|ftp)\\.)[a-z0-9]+(\\.[a-z0-9]+[a-z]+)+([/?].*)?$"
ans:
import java.util.regex.*;
import java.util.*;
class pmr3_recap
{
public static void main(String[] args)
{
Pattern p=Pattern.compile("^((https?|ftp)://|(www|ftp)\\.)[a-z0-9]+(\\.[a-z-9]+[
a-z]+)+([/?].*)?$");
Scanner s=new Scanner(System.in);
System.out.println("Enter the URL");
String data=s.next();
Matcher m=p.matcher(data);
Boolean res=m.matches();
if(res==true)System.out.println("URL is correct format");
else
System.out.println("Incorrect format");
}
}
2.Write a program to accept a three letter word as an input
from the user and display the message, Expression Matched,
if the word starts with the letter, "a", "b", or "c", and
ends with, "at".
Regular expression : bat, cat,
[abc]at
ans: [same program as above just, change the expression]
Pattern p=Pattern.compile("[abc]at");
3.Create a calculator application in Java that will accept two numbers. In addit
ion, the calculator application should allow a user to perform any of the follow
ing operations at a time on the two given numbers:
Addition
Subtraction
Multiplication

Division
Further, you need to implement
assertions to assert that
both the numbers should be greater than 0.

import java.util.*;
class pmr3_recap
{
public static void main(String[] args)
{
int a,b;
Scanner s=new Scanner(System.in);
System.out.println("Enter the numbers");
a=s.nextInt();b=s.nextInt();
assert a>0 && b>0 :"numbers must be greater than zero";
System.out.println("Enter your operations(+,-,*,/)");
String choice=s.next();
int res=0;
if(choice.equals("+")) { res=a+b;}
else if(choice.equals("-")) { res=a-b;}
else if(choice.equals("*")) { res=a*b;}
else if(choice.equals("/")) { res=a/b;}
else {System.out.println("Invalid choice");}
System.out.println(res);
}
}
while compiling:
E:\PGP12_JAVA>javac pmr3_recap.java
E:\PGP12_JAVA>java -ea pmr3_recap
4.Use the Deque object to
match parentheses in a programming statement
(a+b)
[(a*b)+2]
ans:
import java.util.*;
class pmr3_recap
{
public static String check(String str)
{
Deque<Character> deq=new ArrayDeque<Character>();
for(int i=0;i<str.length();i++){
char c=str.charAt(i);
if(c=='(' || c=='{' || c=='[')
deq.push(c);
char d=str.charAt(i);
if((c=='(' && d==')') || (c=='{' && d=='}') ||
(c=='[' && d==']'))

deq.pop();
}
if(deq.isEmpty())
return "Exp matched";
else
return "Exp not matched";
}
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
System.out.println("Enter the expression");
String data=s.next();
System.out.println(check(data));
}
}
5.Process shirt-related transactions for a Duke's Choice store.
Compute the inventory level for a number of shirts.
Then print out the shirt data sorted by description and by inventory count.
import java.util.*;
class shirt
{
int invcount;
String shirtdesc;
public shirt(int i,String d){
invcount=i;
shirtdesc=d;
}
}
class Idsort implements Comparator<shirt>{
public int compare(shirt p1,shirt p2){
if(p1.invcount==p2.invcount){return 0;}
else if(p1.invcount>p2.invcount){return 1;}
else {return -1;}
}
}
class namesort implements Comparator<shirt>{
public int compare(shirt a,shirt b){
return b.shirtdesc.compareTo(a.shirtdesc);
}
}
class demo_9
{
public static void main(String[] args)
{
shirt s1=new shirt(8,"Full sleeves");
shirt s2=new shirt(4,"Half sleeves");
shirt s3=new shirt(9,"Kurtha");
ArrayList a=new ArrayList();
a.add(s1);a.add(s2);a.add(s3);
Collections.sort(a,new Idsort());
System.out.println("Sorting by ID..");
Iterator i=a.iterator();
while(i.hasNext()){
shirt p=(shirt)i.next();
System.out.println(p.invcount+ " "+p.shirtdesc);;}

Collections.sort(a,new namesort());
System.out.println("Sorting by Name..");
Iterator i1=a.iterator();
while(i1.hasNext()){
shirt p=(shirt)i1.next();
System.out.println(p.invcount+ " "+p.shirtdesc);;
}}
}
6.You need to write a program to register the member details to get an entry tic
ket of a disco. The program should accept the member details, such as name, cont
act number, and age. In addition, the program must throw an exception with an ap
propriate message if the age is not within the range of 18 to 55.
hint: create userdefined exception to do it.
import java.util.*;
class AgeException extends Exception
{
public AgeException(String msg){
System.out.println(msg);
}
}
class demo_9
{
public static void main(String[] args)
{
String name,contact_no;
int age;
Scanner s=new Scanner(System.in);
System.out.println("Enter name , contact no and age");
name=s.next();
contact_no=s.next();
age=s.nextInt();
try{
if(age<18 || age>55)
throw new AgeException("age is not within the range of 18 to 55");
}
catch(Exception e){}
}
}

Você também pode gostar