Sunday 2 November 2014

java programming - java programs and solutions - java level - 1 assignment 3

JAVA Assignment 3
                                                                                       BY – Premal Gajjar
1. Write/Create a class called circle which contains following properties and behaviors:- Two private instance variables: radius (of type double) and color (of type String), with default value of 1.0 and "red", respectively.- Two overloaded constructors;- Two public methods: getRadius() and getArea()calculate and display the area of a circle of radius 3.4 & 100 in console window.

Program:

import java.util.Scanner;

class Circle {
    private static double radius=1.0;
    private static String color="Red";
    private String getArea;
           
            public Circle(){
                            radius=100;
                            color="Green";
                            }
            public Circle(double radius,String color) {
                            this.radius=radius;
                            this.color=color;
                            }
           
            public void getRadius() {
                   
                    System.out.println("The Radius of the d_circle is: "+ radius);
                    System.out.println("The color of the d_circle is: "+ color);
                    }
           
            public void getRadius(double radius, String color) {
                           
                            this.radius=radius;
                            this.color=color;
                            System.out.println("The Radius of the circle is: "+ radius);
                            System.out.println("The color of the circle is: "+ color);       
                            }

            public void getArea() {
                           
                            double area =  radius * radius * Math.PI ;
                            System.out.println("The area of the circle is: " + area);
            }
   

    public static void main(String[] args) {
   
    Scanner scan=new Scanner(System.in);   
    Circle c = new Circle();
   
    c.getRadius();
    c.getArea();
   
    System.out.println("Enter Value of Radius and Color name : ");
    Circle c_1=new Circle();
    c_1.getRadius(scan.nextDouble(),scan.next());
    c_1.getArea();
    }
}
   


2. Create a class called Invoice that a hardware store might use to represent an invoice for an item sold a t the store. An Invoice should include four pieces of information as instance variables- part number (type String), a part description(type String), quantity of the item being purchased (type int) and a price per item (double). Your class should have a constructor that initializes the four instance variables. Provide a set and a get method for each instance ariable. In addition, provide a method named getInvoice Amount that calculates the invoice amount (i.e., multiplies the quantity by the price per item), then returns the amount as a double value. If the quantity is not positive, it should be set to 0. If the price per item is notpositive, it should be set to 0.0. Write a test application named
InvoiceTest that demonstrates class Invoice’s capabilities.

Program:

package l_2_2;


import java.io.*;
class Invoice
{
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String part_num;
    String part_des;
    int num_of_items;
    double price_of_items;
    double amount;

    Invoice()
            {
                part_num="1";
                part_des="book";
                num_of_items=1;
                price_of_items=100;
            }

        public String get_part_num() throws IOException
                {
                    System.out.println("Enter part number");
                    part_num=br.readLine();
                    return part_num;
                }
         public String get_part_des()throws IOException
                {
                    System.out.println("Enter part description ");
                    part_des=br.readLine();
                    return part_des;
                }
        public int get_num_of_items()throws IOException
                {
                    System.out.println("Enter number of items ");
                    num_of_items=Integer.parseInt(br.readLine());
                    return num_of_items;
                }
         public double get_price_of_items()throws IOException
                {
                    System.out.println("Enter price of items");
                    price_of_items=Double.parseDouble(br.readLine());
                    return price_of_items;
                }
   
        public double Invoice_amount()
                {
                    amount = price_of_items*num_of_items;
                    amount = (amount>0)?amount:0;
                    return amount;
                }
        public void displayInfo()
                {
                    System.out.printf("part number\t"+part_num+"\tpart description\t"+part_des+"\tnum_of_items\t"+num_of_items+"\tprice_of_items\t"+price_of_items);
                    System.out.printf("\n Amount \t"+amount);
                  //  System.out.println();
                }

public static class InvoiceTest
{
    public static void main(String [] args)throws IOException
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        Invoice inv1 = new Invoice();

        inv1.get_part_num();
        inv1.get_part_des();
        inv1.get_num_of_items();
        inv1.get_price_of_items();
        inv1.Invoice_amount();
        inv1.displayInfo();

    }
}

}



3. Create a class called Employee that includes three pieces of information as instance variables—
a first name (typeString), a last name (typeString) and a monthly salary (double). Your class should have a constructor that initializes the three instance variables. Provide a set and a get method for each instance variable. If the monthly salary is not positive, set it to 0.0. Write a test application named EmployeeTest that demonstrates class Employee’s capabilities. Create two Employee objects and display each object’s yearly salary. Then give each Employee a 10% raise and display each Employee’s yearly salary again.

Program:

package l_2_3;
import java.util.Scanner;// program uses class Scanner

public class Employee {

        private String firstName;
        private String lastName;
        private Double monthlySalary;

        public Employee (double monthly )
                {
                  if ( monthly > 0.00 )
                     monthlySalary = monthly;
                  if ( monthly< 0.00 )
                     monthlySalary = 0.00;
                }

        public void setFirstName( String first )
                {
                  firstName = first;
                }

        public void setLastName( String last )
                {
                  lastName = last;
                }
   
        public void setMonthlySalary( Double monthly )
                {
                  monthlySalary = monthly;
                }

        public String getFirstName()
                {
                  return firstName;
                }

        public String getLastName()
                {
                  return lastName;
                }
   
        public Double getMonthlySalary()
                {
                  return monthlySalary;
                }
   
        public void displayMessage()
                {
                  System.out.printf("%s %s has a Yearly salary of Rs.%.2f\n", getFirstName(), getLastName(), getMonthlySalary()*12 );
                }
        }


package l_2_3;
import java.util.Scanner;

public class classEmployeeTest {

    public static void main( String args[] )
    {
      Scanner input = new Scanner(System.in );

      Employee newEmployee_1 = new Employee( 0.00 );

      System.out.println( "enter Employee's firstname:" );
      String first = input.nextLine();
   
      newEmployee_1.setFirstName( first );
     
      System.out.println( "enter Employee's lastname:" );
      String last = input.nextLine();
      newEmployee_1.setLastName( last );

      System.out.println( "enter Employee's monthly salary:" );
      double monthly = input.nextDouble();
      newEmployee_1.setMonthlySalary( monthly );

      newEmployee_1.displayMessage();   

      Scanner input1 = new Scanner(System.in );

      Employee newEmployee_2 = new Employee( 0.00 );

      System.out.println( "enter Employee's firstname:" );
      String first1 = input1.nextLine();
   
      newEmployee_2.setFirstName( first1 );
     
      System.out.println( "enter Employee's lastname:");
      String last1 = input1.nextLine();
      newEmployee_2.setLastName( last1 );

      System.out.println( "enter Employee's monthly salary:" );
      double monthly1 = input1.nextDouble();
      newEmployee_2.setMonthlySalary( monthly1 );

      newEmployee_2.displayMessage();
    }
    }




4. Create a class called Date that includes three pieces of information as instance variables— month (typeint), a day (typeint) and a year (typeint). Your class should have a constructor that initializes the three instance variables and assumes that the values provided are correct. Provide a set and a get method for each instance variable. Provide a method displayDate that displays the month, day and year separated by forward slashes(/). Write a test application named DateTest that demonstrates classDate’s capabilities.

Program:

package l_2_4;

public class Date {
   
    // fields
    private static int month;
    private static int day;
    private static int year;
 
    // constructor
    public Date(int month, int day, int year){
        this.month = month;
        this.day = day;
        this.year = year;
    }
   
    public void setMonth(int month){
        this.month = month;
    }
   
    public void setDay(int day){
        this.day = day;
    }
   
    public void setYear(int year){
        this.year = year;
    }
   
    public int getMonth(){
        return month;
    }
   
    public int getDay(){
        return day;
    }
   
    public int getYear(){
        return year;
    }
   
    public static String displayDate(){
        return month + "/" + day + "/" + year;
    }
}

    -----------------------------------------------------------

package l_2_4;

public class DateTest {

    public static void main(String[] args) {
        Date newDate = new Date(1, 12, 1991);
        System.out.println("Date is: "+ newDate.displayDate() );
    }
}


5. Create class SavingsAccount. Usea static variable annualInterestRate to store the annual interest rate f or all account holders. Each object of the class contains a private instance variable savingsBalance indicating the amount the saver currently has on deposit.
Provide method calculateMonthlyInterest to calculate the monthly www.oumstudents.tk interest by multiplying the savingsBalance by annualInterestRate divided by 12 thisinterestshould be added to savingsBalance. Provide a static method modifyInterestRate that sets the annualInterestRate to a new v alue.
Write a program to test class SavingsAccount. Instantiate two savingsAccount objects, saver1 and saver, with balances of $2000.00 and $3000.00, respectively. Set annualInterestRate to 4%, then calculate the monthly interest and print the new balances for both savers. Then set the annualInterestRate to 5%, cal culate the next month’s interest and print the new balances for both savers.
Program:

package l_2_5;

class SavingsAccount
{
    static double annualInterestRate;
    private double savingsBalance;

    public SavingsAccount(double balance)
    {
        savingsBalance = balance;
    }

    public double calculateMonthlyInterest()
    {
        return (savingsBalance*annualInterestRate)/12;
    }

    public static void modifyInterestRate(double rate)
    {
        annualInterestRate = rate;
    }


    public static double getannualInterestRate(){return annualInterestRate;}

    public double getsavingsBalance(){return savingsBalance;}

}

public class SavingTest
{
    public static void main(String args[])
    {
        SavingsAccount saver1 = new SavingsAccount(5000.0);
        SavingsAccount saver2 = new SavingsAccount(5000.0);

        SavingsAccount.modifyInterestRate(4);

        System.out.printf("Balance for Saver1 = %.2f\nBalance (with itr) for Saver1 = %.2f\nInterest Rate = %.2f\n\n",saver1.getsavingsBalance(),saver1.getsavingsBalance()+saver1.calculateMonthlyInterest(),SavingsAccount.getannualInterestRate());

        SavingsAccount.modifyInterestRate(5);

        System.out.printf("Balance for Saver2 = %.2f\nBalance (with itr) for Saver2 = %.2f\nInterest Rate = %.2f\n\n",saver2.getsavingsBalance(),saver2.getsavingsBalance()+saver2.calculateMonthlyInterest(),SavingsAccount.getannualInterestRate());
    }
}



6. Create a class called Book to represent a book. A Book should include four pieces of information as instance variablesa book name, an ISBN number, an author name and a publisher. Your class should have a constructor that initializes the four instance variables. Provide a mutator method and accessor method (query method) for each instance variable. Inaddition, provide a method named getBookInfo that returns the description of the book as a String (the description should include all the information about the book).You should use this keyword in member methods and constructor. Write a test application named BookTest to create an array of object for 30 elements for class Book to demonstrate the class Book's capabilities.

Program:

package l_2_6;

import java.util.Scanner;

    class Book {
        private String bookname,authorname,publisher;
        private long ISBN;

public Book() {
}

public Book(String bookname,long ISBN,String authorname,String publisher) {
            this.bookname=bookname;
            this.ISBN=ISBN;
            this.authorname=authorname;
            this.publisher=publisher;
            }

public void setData(String bookname,long ISBN,String authorname,String publisher)
            {
            this.bookname=bookname;
            this.ISBN=ISBN;
            this.authorname=authorname;
            this.publisher=publisher;
            }

public void getData() {
            System.out.println("Book Name = "+bookname);
            System.out.println("ISBN No. = "+ISBN);
            System.out.println("Author Name = "+authorname);
            System.out.println("Publisher = "+publisher);
            }
}
   
    class Program6 {
        public static void main(String[] args) {

            Scanner scan=new Scanner(System.in);
            Book b=new Book();System.out.println("Enter Book name, ISBN number, Author name,Publisher :- ");
            b.setData(scan.next(),scan.nextLong(),scan.next(),scan.next());
            b.getData();
        }
}

No comments: