Slip9

 Q1) Define a “Clock” class that does the following ; a. Accept Hours, Minutes and Seconds b. Check the validity of numbers c. Set the time to AM/PM mode Use the necessary constructors and methods to do the above task [10 marks] 

import java.util.*;

class Clock {


   int hr,min,sec; //store hours

   public Clock (int hr, int min, int sec) 

   {

      this.hr=hr;

this.min=min;

this.sec=sec;

   }

   public boolean checktime() 

   {

boolean hflag,mflag,sflag;

      if (hr>=0 && hr < 24)

hflag=true;

        else

hflag=false;

if (min >=0 && min < 60)

          mflag=true;

        else

mflag=false;

      if (sec >=0 && sec < 60)

          sflag=true;

        else

sflag=false;

if(hflag&&mflag&&sflag)

return true;

else

return false;

   }


   public void setAMPM()

   {

      if(hr>=0 && hr<12)

System.out.println("Time  = "+hr+":"+min+":"+sec+" AM");

else

System.out.println("Time  = "+hr+":"+min+":"+sec+" PM");

   }


   }


public class Slip9_1

{  

        public static void main(String args[])  

    {  

    Scanner s=new Scanner(System.in);

System.out.println("Enter Hours, Minutes and Seconds");

int hh=s.nextInt();

int mm=s.nextInt();

int ss=s.nextInt();

Clock c=new Clock(hh,mm,ss);

if((c.checktime())==true)

c.setAMPM();

else

System.out.println("Invalid Time");

}

}

Q2) Write a program to using marker interface create a class Product (product_id, product_name, product_cost, product_quantity) default and parameterized constructor. Create objectsof class product and display the contents of each object and Also display the object count.

//slip9_2

import java.util.Scanner;  

interface productdetail

{

}

public class Product implements productdetail   

{

static int cnt;  

int pid,qty;  

String pname;  

double pcost;  

public Product (int pid, String pname, double pcost, int qty)  

{  

cnt++;

this.pid = pid;  

this.pname = pname;  

this.pcost = pcost;

this.qty=qty;  

public Product ()  

cnt++; 

this.pid = 0;  

this.pname = "";  

this.pcost = 0;

this.qty=0;  

public String toString()   

{  

return pid+"\t"+pname+"\t"+pcost+"\t"+qty; 

}  

public static void main (String args[])    

{  

//reading values of the product from the user  

Scanner sc = new Scanner(System.in);  


System.out.println("Enter Total No. of Product");

                int n=sc.nextInt();

Product[] p=new Product [n];

                 

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

{

    System.out.print("Enter product ID: ");  

int pid = sc.nextInt();  

System.out.print("Enter product name: ");  

String pname = sc.next();  

System.out.print("Enter product Cost: ");  

double pcost = sc.nextDouble();  

System.out.print("Enter product Quantity: ");  

int q = sc.nextInt(); 

p[i]=new Product(pid, pname, pcost,q);

}

System.out.println("-------Product Detail--------");  

System.out.println("Id Name Cost Quantity"); 

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

System.out.println(p[i]);

//invoking the method to print detail  

System.out.println("Total no. of. Product objects = "+Product.cnt);

}  

}  


Comments

Popular posts from this blog

Slip1

Slip3

Slip2