TYBCS Java Assignment 1 - Sem I

SET A 

1.  Using javap, view the methods of the following classes from the lang package:  java.lang.Object , java.lang.String and java.util.Scanner.  

2.  Compile sample program 2. Type the following command and view the bytecodes. javap -c MyClass 

SET B 

1.  Write a java program to display the system date and time in various formats shown below: 
Current date is  : 31/07/2015 
Current date is  : 07-31-2015 
Current date is  : Friday July 31 2015 
Current date and time is  : Fri July 31 16:25:56 IST 2015 
Current date and time is  :  31/07/15 16:25:56 PM +0530  
Current time is  :   16:25:56  
Current week of year is : 31 
Current week of month : 5 
Current day of the year is : 212 
Note: Use java.util.Date and java.text.SimpleDateFormat class 

2.  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. 

Post a Comment

15 Comments

  1. Assignment 1 set C please ... thanks for this stuff.. great work man...

    ReplyDelete
  2. Slip 3_2: Define a class patient (patient_name, patient_age,
    patient_oxy_level,patient_HRCT_report). Create an object of patient. Handle
    appropriate exception while patient oxygen level less than 95% and HRCT scan
    report greater than 10, then throw user defined Exception “Patient is Covid Positive(+)
    and Need to Hospitalized” otherwise display its information.

    import java.io.*;
    class CovidException extends Exception{
    public CovidException(){
    System.out.println("Patient is Covid Positive and needs to be hospitalized");
    }
    }
    class Patient{
    String name;
    int age;
    double level,hrct;
    public Patient(String name,int age,double level,double hrct)
    {
    this.name=name;
    this.age=age;
    this.level=level;
    this.hrct=hrct;
    }
    public static void main(String[] args)throws IOException
    {
    String name;
    int age;
    double level,hrct;
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Enter name: ");
    name=br.readLine();
    System.out.println("Enter the age: ");
    age=Integer.parseInt(br.readLine());
    System.out.println("Oxygen level: ");
    level=Double.parseDouble(br.readLine());
    System.out.println("HRCT report: ");

    hrct=Double.parseDouble(br.readLine());
    Patient ob=new Patient(name,age,level,hrct);
    try{
    if(ob.level<95 && ob.hrct>10)
    throw new CovidException();
    else
    System.out.println("Patient Info: \n"+"Name: "+ob.name+"\nAge: "+ob.age+"\nHRCT
    report: "+ob.hrct+"\nOxygen level:"
    +ob.level);
    }catch(CovidException e){
    }
    }
    }

    ReplyDelete
  3. Slip no:-[1] [11] [23] [25] [29]
    Write a program to calculate perimeter and area of rectangle.
    (hint : area = length * breadth , perimeter=2*(length+breadth))


    import java.io.*;
    class area
    {
    public static void main(String args[])
    {
    int a,b,c,d,perimeter,area;
    a=c=5;
    b=d=2;
    area=a*b;
    perimeter=2*(a+b);
    System.out.println("Area of rectangle="+area);
    System.out.println("Area of
    perimeter="+perimeter);
    }
    }

    ReplyDelete
  4. Slip no:- [3] [17] [20] [27]
    Write a program to accept the array element and display in reverse order.


    class ReverseArray
    {
    public static void main(String args[])
    {
    int a[]=new int[]{1,2,3,4,5};
    {
    System.out.println("Original array");
    for(int i=0; i=0;i--)
    {System.out.println(a[i]);
    }
    }
    }
    }

    ReplyDelete
  5. Slip no :- [4 ]

    1. Write a java program to display the system date and time in various formats shown
    below: [10]
    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 15:25:59 IST 2021
    Current date and time is : 31/08/21 15:25:59 PM +0530
    Current time is : 15:25:59
    Current week of year is : 35
    Current week of month : 5
    Current day of the year is : 243
    Note: Use java.util.Date and java.text.SimpleDateFormat class


    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.Locale;
    public class Newclass {
    public static void main(String[] args) {
    Date date = new Date();
    SimpleDateFormat formatter = new
    SimpleDateFormat("dd/MM/yyyy");
    String strDate = formatter.format(date);
    System.out.println("Current date is: "+strDate);
    formatter = new SimpleDateFormat("MM-dd-
    yyyy");
    strDate = formatter.format(date);
    System.out.println("Current date is: "+strDate);
    formatter = new SimpleDateFormat("EEEEEE
    MMMM dd yyyy");
    strDate = formatter.format(date);
    System.out.println("Current date is: "+strDate);
    formatter = new SimpleDateFormat("E MMMM
    dd HH:mm:ss z yyyy");
    strDate = formatter.format(date);
    System.out.println("Current date and time is:
    "+strDate);
    formatter = new SimpleDateFormat("dd/MM/yy
    HH:mm:ss a Z");
    strDate = formatter.format(date);
    System.out.println("Current date and time is:
    "+strDate);
    formatter = new
    SimpleDateFormat("hh:mm:ss");
    strDate = formatter.format(date);
    System.out.println("Current time is: "+strDate);
    formatter = new SimpleDateFormat("w");
    strDate = formatter.format(date);
    System.out.println("Current week of year is:
    "+strDate);
    formatter = new SimpleDateFormat("W");
    strDate = formatter.format(date);
    System.out.println("Current week of the month
    is: "+strDate);
    formatter = new SimpleDateFormat("D");
    strDate = formatter.format(date);
    System.out.println("Current day of the year:
    "+strDate);
    }
    }

    ReplyDelete
  6. Slip no:- [ 5]
    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.


    public class MyNumber
    {
    private int x;
    public MyNumber()
    {
    x=0;
    }
    public MyNumber(int x)
    {
    this.x=x;
    }
    public boolean isNegative()
    {
    if(x<0)
    return true;
    else return false;
    }
    public boolean isPositive()
    {
    if(x>0)
    return true;
    else return false;
    }
    public boolean isZero()
    {
    if(x==0)
    return true;
    else return false;
    }
    public boolean isOdd()
    {
    if(x%2!=0)
    return true;
    else return false;
    }
    public boolean isEven()
    {
    if(x%2==0)
    return true;
    else return false;
    }
    public static void main(String [] args) throws
    ArrayIndexOutOfBoundsException
    {
    int x=Integer.parseInt(args[0]);
    MyNumber m=new MyNumber(x);
    if(m.isNegative())
    System.out.println("Number is Negative");
    if(m.isPositive())
    System.out.println("Number is Positive");
    if(m.isEven())
    System.out.println("Number is Even");
    if(m.isOdd())
    System.out.println("Number is Odd");
    if(m.isZero())
    System.out.println("Number is Zero");
    }
    }

    ReplyDelete
  7. Slip no :- [6] [9] [15 ] [28]
    Write a java program to accept 5 numbers using command line arguments sort and
    display them.

    import java.io.*;
    class sort
    {
    public static void main(String[] args) throws
    IOException
    {
    BufferedReader br= new BufferedReader(new
    InputStreamReader(System.in));
    int [] arr=new int[5];
    for(int i=0;i<5;i++) //Array Initialization
    {
    arr[i]=Integer.parseInt(br.readLine());
    }
    int temp = 0; //Temporary variable to store the
    element
    for (int i = 0; i < 5; i++) //Holds each Array
    element
    {
    for (int j = i+1; j < 5; j++) //compares with
    remaining Array elements
    {
    if(arr[i] > arr[j]) //Compare and swap
    {
    temp = arr[i];
    arr[i] = arr[j];
    arr[j] = temp;
    }
    }
    }
    System.out.println();
    //Displaying elements of array after sorting
    System.out.println("Elements of array sorted in
    ascending order: ");
    for (int i = 0; i < 5; i++)
    {
    System.out.print(arr[i] + " ");
    }
    }
    }

    ReplyDelete
  8. Slip no :- [8] [16]

    Define Student class(roll_no, name, percentage) to create n objects of the Student class.
    Accept details from the user for each object. Define a static method “sortStudent” which
    sorts the array on the basis of percentage.

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    class Student
    {
    int rollno;
    String name;
    float per;
    static int count;
    Student(){}
    Student(String n,float p)
    {
    count++;
    rollno=count;
    name=n;
    per=p;
    }
    void display()
    {
    System.out.println(rollno+"\t"+name+"\t"+per);
    }
    float getper()
    {
    return per;
    }
    static void counter()
    {
    System.out.println(count+" object is created");
    }
    public static void sortStudent(Student s[],int n)
    {
    for(int i=n-1;i>=0;i--)
    {
    for(int j=0;js[j+1].getper())
    {
    Student t=s[j];
    s[j]=s[j+1];
    s[j+1]=t;
    }
    }
    }
    for(int i=0;i<n;i++)
    s[i].display();
    }
    }
    class Studentclass
    {
    public static void main(String args[]) throws
    IOException
    {
    BufferedReader br=new BufferedReader(new
    InputStreamReader(System.in));
    System.out.println("Enter no. of Student:");
    n=Integer.parseInt(br.readLine());
    Student p[]=new Student[n];
    for(int i=0;i<n;i++)
    {
    System.out.print("Enter Name:");
    String name=br.readLine();
    System.out.print("Enter percentage:");
    float per=Float.parseFloat(br.readLine());
    p[i]=new Student(name,per);
    p[i].counter();
    }
    Student.sortStudent(p,Student.count);
    }
    }

    ReplyDelete
  9. Slip no :- [10]

    Write a java program that take input as a person name in the format of first, middle
    and last name and then print it in the form last, first and middle name, where in the
    middle name first character is capital letter.

    import java.util.*;
    class person
    {
    String fname,mname,lname;
    int len;
    void accept()
    {
    System.out.println("Enter First Name :");
    Scanner s=new Scanner(System.in);
    fname=s.next();
    System.out.println("Enter Middle Name :");
    mname=s.next();
    System.out.println("Enter Last Name :");
    lname=s.next();
    len=mname.length();
    String f=mname.substring(0,1);
    String l=mname.substring(1,len);
    f=f.toUpperCase();
    mname=f+l;
    }
    void display()
    {
    System.out.println("Last Name :"+lname);
    System.out.println("First Name :"+fname);
    System.out.println("Middle Name :"+mname);
    }
    public static void main(String a[])
    {
    person p=new person();
    p.accept();
    p.display();
    }
    }

    ReplyDelete
  10. Slip no:- [12] [13]

    Write a program for multilevel inheritance such that country is inherited from continent.
    State is inherited from country. Display the place, state, country and continent.

    import java.io.InputStreamReader;
    import java.io.BufferedReader;
    import java.io.IOException;
    class Continent
    {
    String con;
    InputStreamReader i = new
    InputStreamReader(System.in);
    BufferedReader r = new BufferedReader(i);
    void con_input() throws IOException
    {
    System.out.println("Enter Continent Name: ");
    con = r.readLine();
    }
    }
    class Country extends Continent
    {
    String cou ;
    void cou_input() throws IOException
    {
    System.out.println("Enter Country Name: ");
    cou = r.readLine();
    }
    }
    class State extends Country
    {
    String sta;
    void sta_input() throws IOException
    {
    System.out.println("Enter State Name: ");
    sta = r.readLine();
    }
    }
    class Main extends State
    {
    String pla;
    void pla_input()throws IOException
    {
    System.out.println("Enter Place Name : ");
    pla = r.readLine();
    }
    public static void main( String argsp[] )throws
    IOException
    {
    Main s = new Main();
    s.con_input();
    s.cou_input();
    s.sta_input();
    s.pla_input();
    System.out.println("\n\nContinent: "+s.con);
    System.out.println("Country: "+s.cou);
    System.out.println("State: "+s.sta);
    System.out.println("Place :" + s.pla);
    }
    }

    ReplyDelete
  11. Slip No:- [14]
    Q No 2 :- [13]
    Write a menu driven program to perform the following operations [10]
    i. Calculate the volume of cylinder. (hint : Volume: π × r2 × h)
    ii. Find the factorial of given number.
    iii. Check the number is Armstrong or not.
    iv. Exit


    ##Slip 13 Q2) & slip 14 Q1).
    import java.util.Scanner;
    public class menudriven
    {
    public static void main(String[] args)
    {
    int choice;//for storing users choice
    double radius;
    double height;
    double volume;
    Scanner sc=new Scanner(System.in);//Creating
    object of the scanner class
    //displaying the menu
    System.out.println("1:Volume of cylinder");
    System.out.println("2:Factorial of number");
    System.out.println("3:Number is armstrong or
    not");
    System.out.println("4:Exit");
    lp : while(true)//labelling the while loop
    {
    System.out.println("Make your choice");
    choice=sc.nextInt();//reading users choice
    switch(choice)
    {
    case 1:
    //take input from the user
    //create an instance of the scanner class
    Scanner s=new Scanner(System.in);
    System.out.println("Enter the radius:");
    radius=s.nextDouble();
    System.out.println("Enter the height:");
    height=s.nextDouble();
    volume=(22*(radius*radius)*height/7);
    System.out.println("volume of cylinder
    is:"+volume);
    break;
    case 2:
    Scanner a=new Scanner(System.in);
    System.out.println("Enter the number:");
    int num=a.nextInt();
    int i=1,fact=1;
    while(i<=num)
    {
    fact=fact*i;
    i++;
    }
    System.out.println("Factorial of the
    number:"+fact);
    break;
    case 3:
    int temp,totalDigit=0,res=0,rem,pow;
    Scanner b=new Scanner(System.in);
    System.out.println("Enter the number:");
    num=b.nextInt();
    temp=num;
    while(num>0)
    {
    num=num/10;
    totalDigit++;
    }
    num=temp;
    while(num>0)
    {
    rem=num%10;
    pow=1;
    i=0;
    while(i<totalDigit)
    {
    pow=pow*rem;
    i++;
    }
    res=res+pow;
    num=num/10;
    }
    if(res==temp)
    System.out.println("\n Armstrong number:");
    else
    System.out.println("\n Not an Armstrong
    number:");
    break;
    case 4:System.out.println("EXIT");
    break;
    }
    }
    }
    }

    ReplyDelete
  12. Slip no :- [19]
    Write a program to read a text file “sample.txt” and display the contents of a file in
    reverse order and also original contents change the case (display in upper case).

    import java.io.*;
    import java.util.*;
    class SetAq2
    {
    public static void main(String[] args)throws
    IOException
    {
    FileReader file=new FileReader("a.txt");
    Scanner sc=new Scanner(file);
    String s;
    while(sc.hasNext())
    {
    StringBuffer sb=new StringBuffer();
    s=sc.next();
    String s1=s.toUpperCase();
    sb.append(s1);
    sb.reverse();
    System.out.println(sb);
    }
    }
    }

    ReplyDelete
  13. Slip no:-[ 21]
    Define an abstract class Staff with protected members id and name. Define a
    parameterized constructor. Define one subclass OfficeStaff with member department.
    Create n objects of OfficeStaff and display all details
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    abstract class Staff{
    String name,address;
    }
    class FullTimeStaff extends Staff{
    String department;
    double salary;
    public void accept() throws IOException{
    System.out.println("Enter the name, address,
    department and salary: ");
    BufferedReader br=new BufferedReader(new
    InputStreamReader(System.in));
    name=br.readLine();
    address=br.readLine();
    department=br.readLine();
    salary=Double.parseDouble(br.readLine());
    }
    public void display(){
    System.out.println("Name: "+name);
    System.out.println("Address: "+address);
    System.out.println("Department:
    "+department);
    System.out.println("Salary: "+salary);
    System.out.println("----------------------");
    }
    }
    class PartTimeStaff extends Staff{
    int hours, rate;
    public void accept() throws IOException{
    System.out.println("Enter the name, address,
    No of working hours and rate per hour: ");
    BufferedReader br=new BufferedReader(new
    InputStreamReader(System.in));
    name=br.readLine();
    address=br.readLine();
    hours=Integer.parseInt(br.readLine());
    rate=Integer.parseInt(br.readLine());
    }
    public void display(){
    System.out.println("Name: "+name);
    System.out.println("Address: "+address);
    System.out.println("No of Working Hours:
    "+hours);
    System.out.println("Rate per hour: "+rate);
    System.out.println("----------------------");
    }
    }
    class stafftime{
    public static void main(String [] args) throws
    IOException{
    int i;
    System.out.println("Select Any One: ");
    BufferedReader br=new BufferedReader(new
    InputStreamReader(System.in));
    System.out.println("1.Full Time Staff");
    System.out.println("2.Part Time Satff");
    int ch=Integer.parseInt(br.readLine());
    switch(ch){
    case 1:
    System.out.println("Enter the number of Full
    Time Staff: ");
    int n=Integer.parseInt(br.readLine());
    FullTimeStaff [] l=new FullTimeStaff[n];
    for(i=0;i<n;i++){
    l[i]=new FullTimeStaff();
    l[i].accept();
    }
    for(i=0;i<n;i++){
    l[i].display();
    }
    break;
    case 2:
    System.out.println("Enter the number of Part
    Time Staff: ");
    int m=Integer.parseInt(br.readLine());
    PartTimeStaff [] h=new PartTimeStaff[m];
    for(i=0;i<m;i++){
    h[i]=new PartTimeStaff();
    h[i].accept();
    }
    for(i=0;i<m;i++){
    h[i].display();
    }
    break;
    }
    }
    }

    ReplyDelete
  14. Slip no:- [22][24] [26] [30]

    Write a program to find the cube of given number using function interface.


    import java.util.Scanner;
    @FunctionalInterface
    interface CubeCalculator{
    public void print(int num1); }
    public class Cube{
    public static void main(String[] args){
    CubeCalculator p=n-
    >System.out.println("Cube is : "+n*n*n);
    p.print(5); }}

    ReplyDelete