Q1 Create an abstract class “order” having members id,description.
Create two subclasses
“Purchase Order” and “Sales Order” having members customer name and Vendor name
[Link] methods accept and display in all cases. Create 3 objects each of Purchase
Order and Sales Order and accept and display details
import [Link];
import [Link];
import [Link];
abstract class Order{
String id,description;
}
class PurchaseOrder extends Order{
String Customername,Vendorname;
public void accept() throws IOException{
[Link]("Enter the id,description,names of customers and vendors: ");
BufferedReader br=new BufferedReader(new InputStreamReader([Link]));
id=[Link]();
description=[Link]();
Customername=[Link]();
Vendorname=[Link]();
}
public void display(){
[Link]("id: "+id);
[Link]("Description: "+description);
[Link]("Customername: "+Customername);
[Link]("Vendorname: "+Vendorname);
[Link]("----------------------");
}
}
class SalesOrder extends Order{
String Customername,Vendorname;
public void accept() throws IOException{
[Link]("Enter the id,description,names of customers and vendors: ");
BufferedReader br=new BufferedReader(new InputStreamReader([Link]));
id=[Link]();
description=[Link]();
Customername=[Link]();
Vendorname=[Link]();
}
public void display(){
[Link]("id: "+id);
[Link]("Description: "+description);
[Link]("Customername: "+Customername);
[Link]("Vendorname: "+Vendorname);
[Link]("----------------------");
}
}
public class Main {
public static void main(String [] args) throws IOException{
int i;
[Link]("Select Any One: ");
BufferedReader br=new BufferedReader(new InputStreamReader([Link]));
[Link]("[Link] Order");
[Link]("[Link] Order");
int ch=[Link]([Link]());
switch(ch){
case 1:
[Link]("Enter the number of purchase Orders: ");
int n=[Link]([Link]());
PurchaseOrder [] l=new PurchaseOrder[n];
for(i=0;i<n;i++){
l[i]=new PurchaseOrder();
l[i].accept();
}
for(i=0;i<n;i++){
l[i].display();
[Link] ("Object is created");
}
break;
case 2:
[Link]("Enter the number of sales orders: ");
int m=[Link]([Link]());
SalesOrder [] h=new SalesOrder[m];
for(i=0;i<m;i++){
h[i]=new SalesOrder();
h[i].accept();
}
for(i=0;i<m;i++){
h[i].display();
[Link](" Object is created ");
}
break;
}
}
}
Q2 Write a program to using marker interface create a class product(product_id, product_name,
product_cost, product_quantity) define a default and parameterized constructor. Create objects of
class product and display the contents of each object and Also display the object count.
import [Link].*;
interface ProductMarker
{
}
class Product implements ProductMarker
{
int id;
String name;
int cost;
int quantity;
int count;
Product(){
id=0;
name=" ";
cost=0;
quantity=0;
}
Product(int id, String name, int cost, int quantity){
[Link]=id;
[Link]=name;
[Link]=cost;
[Link]=quantity;
[Link]++;
}
}
public class Products
{
public static void main(String[] args)
{
int count=0;
Scanner a = new Scanner([Link]);
[Link]("How many product ?");
int number = [Link]();
[Link]("\n");
Product products[] = new Product[number];
[Link]("Enter Product data");
for(int k=0; k<number; k++)
{
[Link]("Product Id ");
int id =[Link]();
[Link]("Product name ");
String name = [Link]();
[Link]("Product cost ");
int cost = [Link]();
[Link]("Product qantity ");
int quantity = [Link]();
[Link]("\n");
products[k] = new Product(id, name, cost, quantity);
count++;
}
//Testing for marker interface
if(products[0] instanceof ProductMarker){
[Link]("Class is using ProductMarker");
}
[Link](" Product details\n");
for(Product product:products)
{
[Link]("Product Id " + [Link]);
[Link]("Product name " + [Link]);
[Link]("Product cost " + [Link]);
[Link]("Product qantity " + [Link]);
[Link]("\n");
}
[Link]("Total object is "+count);
}
}
Q3
Define a class MyNumber having one private int data member. Write a default constructor to
initialize it to 0 and another constructor to initialize it to a value (Use this). Write methods
isNegative, isPositive, isZero, isOdd, isEven. Create an object in main. Use command line
arguments to pass a value to the object
(Hint : convert string argument to integer) and perform the above tests. Provide javadoc
comments for all constructors and methods and generate the html help file.
import [Link].*;
import [Link].*;
class MyNumber
{
private int data;
MyNumber()
{
[Link] = 0;
}
MyNumber(int data)
{
this();
int temp = data;
}
void isZero(int temp)
{
if(temp==0)
[Link]("Given number is Zero . ");
}
void isEven(int temp)
{
if(temp%2==0)
[Link]("Given number is Even Number. ");
}
void isOdd(int temp)
{
if(temp%2!=0)
[Link]("Given number is Odd Number. ");
}
void isNegative(int temp)
{
if(temp < 0)
[Link]("Given number is Negative Number. ");
}
void isPositive(int temp)
{
if(temp >= 0)
[Link]("Given number is Positive Number. ");
}
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
int n = [Link]();
MyNumber ob = new MyNumber();
[Link](n);
[Link](n);
[Link](n);
[Link](n);
[Link](n);
}
Q4
Q.2 Write a java program to display the system date and time in various formats shown below:
Current date is : 31/08/2021 Current date is : 08-31-2021
Current date is : Tuesday August 31 2021
Current date and time is : Fri August 31 [Link] IST 2021 Current date and time is :
31/08/21 [Link] PM +0530 Current time is : [Link]
Current week of year is : 35 Current week of month : 5 Current day of the year is : 243
Note: Use [Link] and [Link] class
import [Link];
import [Link];
import [Link];
import [Link];
public class NewClass {
public static void main(String[] args) {
Date date = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
String strDate = [Link](date);
[Link]("Current date is: "+strDate);
formatter = new SimpleDateFormat("MM-dd-yyyy");
strDate = [Link](date);
[Link]("Current date is: "+strDate);
formatter = new SimpleDateFormat("EEEEEE MMMM dd yyyy");
strDate = [Link](date);
[Link]("Current date is: "+strDate);
formatter = new SimpleDateFormat("E MMMM dd HH:mm:ss z yyyy");
strDate = [Link](date);
[Link]("Current date and time is: "+strDate);
formatter = new SimpleDateFormat("dd/MM/yy HH:mm:ss a Z");
strDate = [Link](date);
[Link]("Current date and time is: "+strDate);
formatter = new SimpleDateFormat("hh:mm:ss");
strDate = [Link](date);
[Link]("Current time is: "+strDate);
formatter = new SimpleDateFormat("w");
strDate = [Link](date);
[Link]("Current week of year is: "+strDate);
formatter = new SimpleDateFormat("W");
strDate = [Link](date);
[Link]("Current week of the month is: "+strDate);
formatter = new SimpleDateFormat("D");
strDate = [Link](date);
[Link]("Current day of the year: "+strDate);
}
}
Q.2 Define class EmailId with members ,username and password. Define default and
parameterized constructors. Accept values from the command line Throw user defined
exceptions – “InvalidUsernameException” or “InvalidPasswordException” if the username and
password are invalid.
package sikshapath; //create your own package
import [Link].*;
import [Link];
import [Link];
import [Link];
class EmailId
{
String domainID,username,password;
EmailId()
{
domainID="K7";
username="K";
password="constructc000";
}
EmailId(String user,String pass,String domID)
{
domainID=domID;
username=user;
password=pass;
}
public static void main(String[] args) {
String user,pass, domID;
int p=-1,up=-1,u=-1,d=-1;
EmailId obj=new EmailId();
Scanner sc= new Scanner([Link]);
[Link]("Enter username: ");
user=[Link]();
[Link]("\nEnter password: ");
pass=[Link]();
[Link]("\nEnter Domain ID: ");
domID=[Link]();
[Link]("Enter a date in dd/mm/yyyy format :");
String date = [Link]();
EmailId obj1=new EmailId(user,pass,domID);
if(([Link]).equals([Link]))
{
u=1;
if([Link]([Link]))
{
up=1;
}
else
{
p=0;
}
}
else
{
u=0;
}
if(dateValidation(date)==true)
d=1;
else
d=0;
if(d==1)
{
if(u==0)
{
try{
throw new InvalidUsernameException(user);
}
catch (Exception e) {
[Link](e) ;
}
}
if (p==0){
{
try{
throw new InvalidPasswordException(pass);
}
catch (Exception e) {
[Link](e) ;
}
}
}
if(up==1)
{
[Link]("Valid email id");
}
}
else{
[Link]("Not a Valid email id");
}
}
public static boolean dateValidation(String date)
{
boolean status = false;
if (checkDate(date)) {
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
[Link](false);
try {
[Link](date);
status = true;
} catch (Exception e) {
status = false;
}
}
return status;
}
static boolean checkDate(String date) {
String pattern = "(0?[1-9]|[12][0-9]|3[01])\\/(0?[1-9]|1[0-2])\\/([0-9]{4})";
boolean flag = false;
if ([Link](pattern)) {
flag = true;
}
return flag;
}
}
class InvalidUsernameException extends Exception{
String num1;
InvalidUsernameException(String num2) {
num1=num2;
}
public String toString(){
return ("exception in thread ‘main’ InvalidUsernameException: Username: "+ num1 + " doesn’t match") ;
}
}
class InvalidPasswordException extends Exception{
String num1;
InvalidPasswordException(String num2) {
num1=num2;
}
public String toString(){
return ("exception in thread ‘main’ InvalidPasswordException: Username: "+ num1 + " doesn’t match") ;
}
}
Q.2 Define a class MyDate (day, month, year) with methods to accept and display a MyDate
object. Accept date as dd, mm, yyyy. Throw user defined exception “InvalidDateException” if
the date is invalid.
Examples of invalid dates : 03 15 2019, 31 6 2000, 29 2 2021
import java .io.*;
class InvalidDateException extends Exception
{
}
class MyDate
{
int day,mon,yr;
void accept(int d,int m,int y)
{
day=d;
mon=m;
yr=y;
}
void display()
{
[Link]("Date is valid :
"+day+"/"+mon+"/"+yr);
}
}
class DateMain
{
public static void main(String arg[]) throws Exception
{
[Link]("Enter Date : dd mm yyyy ");
BufferedReader br = new BufferedReader(new
InputStreamReader([Link]));
int day=[Link]([Link]());
int mon=[Link]([Link]());
int yr=[Link]([Link]());
int flag=0;
try
{
if(mon<=0 || mon>12)
throw new InvalidDateException();
else
{
if(mon==1 || mon==3 || mon==5 || mon==7 ||
mon==8 || mon==10 || mon==12)
{
if(day>=1 && day <=31)
flag=1;
else
throw new InvalidDateException();
}
else if (mon==2)
{
if(yr%4==0)
{
if(day>=1 && day<=29)
flag=1;
else throw new InvalidDateException();
}
else
{
if(day>=1 && day<=28)
flag=1;
else throw new InvalidDateException();
}
}
else
{
if(mon==4 || mon == 6 || mon== 9 ||
mon==11)
{
if(day>=1 && day <=30)
flag=1;
else throw new InvalidDateException();
}
}
}
if(flag== 1)
{
MyDate dt = new MyDate();
[Link](day,mon,yr);
[Link]();
}
}
catch (InvalidDateException mm)
{
[Link]("Invalid Date");
}
Q.2 Create the following GUI screen using appropriate
layout managers. Accept the name, class ,
hobbies of the user and apply the changes and display
the selected options in a text box.
HomejavaJava Program to create a GUI and accept the name,
class, hobbies of the user and display the selected options in a text
box - IProgramX
Java Program to create a GUI
and accept the name, class,
hobbies of the user and display
the selected options in a text
box - IProgramX
by - IProgram X on - July 03, 2018
Q. Create the following GUI screen using appropriate
layout managers. Accept the name, class , hobbies of
the user and display the selected options in a text box.
Program:
import [Link].*;
import [Link].*;
import [Link].*;
class Swing2 extends JFrame implements
ActionListener
{
JLabel l1,l2,l3;
JButton b;
JRadioButton r1,r2,r3;
JCheckBox c1,c2,c3;
JTextField t1,t2;
ButtonGroup b1;
JPanel p1,p2;
static int cnt;
private StringBuffer s1=new StringBuffer();
Swing2()
{
b1=new ButtonGroup();
p1=new JPanel();
p2=new JPanel();
b=new JButton("Clear");
[Link](this);
r1=new JRadioButton("FY");
r2=new JRadioButton("SY");
r3=new JRadioButton("TY");
[Link](r1);
[Link](r2);
[Link](r3);
[Link](this);
[Link](this);
[Link](this);
c1=new JCheckBox("Music");
c2=new JCheckBox("Dance");
c3=new JCheckBox("Sports");
[Link](this);
[Link](this);
[Link](this);
l1=new JLabel("Your Name");
l2=new JLabel("Your Class");
l3=new JLabel("Your Hobbies");
t1=new JTextField(20);
t2=new JTextField(30);
[Link](new GridLayout(5,2));
[Link](l1);[Link](t1);
[Link](l2);[Link](l3);
[Link](r1);[Link](c1);
[Link](r2); [Link](c2);
[Link](r3);[Link](c3);
[Link](new FlowLayout());
[Link](b);
[Link](t2);
setLayout(new BorderLayout());
add(p1,[Link]);
add(p2,[Link]);
setSize(400,200);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e)
{
if([Link]()==r1)
{
cnt++;
if(cnt==1)
{
String s =[Link]();
[Link]("Name = ");
[Link](s);
}
[Link](" Class = FY");
}
else if([Link]()==r2)
{
cnt++;
if(cnt==1)
{
String s =[Link]();
[Link]("Name = ");
[Link](s);
}
[Link](" Class = SY");
}
else if([Link]()==r3)
{
cnt++;
if(cnt==1)
{
String s =[Link]();
[Link]("Name = ");
[Link](s);
}
[Link](" Class = TY");
}
else if([Link]()==c1)
{
[Link](" Hobbies = Music");
}
else if([Link]()==c2)
{
[Link](" Hobbies = Dance");
}
else if([Link]()==c3)
{
[Link](" Hobbies = Sports");
}
[Link](new String(s1));
// [Link](s2);
if([Link]()==b)
{
[Link](" ");
[Link](" ");
}
public static void main(String arg[])
{
Swing2 s=new Swing2();
}
}
Java - 2…
TD0512
138 documents
Course: Advance Java
University: Savitribai Phule Pune University
Download
<AW
Clear=new
[Link](this);
Name:-Omkar
Class:-TYBsc(cs)
1)
password.
appropriate
ithe
import
}Iclass
implements
L
TextField
Button
Panel
attempt=0;
public
{p
nametext=new
passtext
msg=new
[Link](c);
[Link](false);
Smport
nvalidPasswordException()
abel
ublic
=new
write
same
charTextFields.
”InvalidPasswordException
uname,upass;
uname=new
upass=new
login=newp;
);void
class
login,Clear;
[Link].*;
[Link].*;
Panel();
‘aint
*=new
nametext;
passtext,msg;
program
TextField(10);
If
login()
[Link](”
c= message.
ActionListener
PasswordDemo
the
.S.
TextField(20);
TextField(20);
‘Button(
Button(
;Label user
Label(Barve
“to
“[Link]:-TD51
design
name
User
(“Login”
“Clear”
Password:
Use
User Name:can
and
aextends
);name screen
have
password
extends
3Frame
using
login
Exception
are
Awt
“,[Link]);
”and chances
not
isthat
,[Link]);
Password same,
not will
[Link]
raise
Useaan
clear
user
Exception
name
buttonand
to
with
clear