Vous êtes sur la page 1sur 6

Programs on Functions

Std XI
• /*Q4. Define a class named movieMagic with the • // Other member methods • // display
following description:


Instance variables/data members:
int year – to store the year of release of a movie
• public void accept() • public void display()
• String title – to store the title of the movie
• float rating – to store the popularity rating of the • { • {
movie (minimum rating=0.0 and maximum
rating=5.0)
• Member methods: • Scanner sc = new Scanner(System.in); • String message = "";
• (i) movieMagic() – Default constructor to
initialize numeric data members to 0 and String


data members to “”.
(ii) void accept() – To input and store year, title
• if(rating <= 2.0F)
and rating • System.out.print("Enter the year of the movie : ");
• (iii) void display() – To display the title of the
movie and a message based on the rating as per
• message = "Flop";
the table below. • this.year = Integer.parseInt(sc.nextLine());


RATING
0.0 to 2.0
MESSAGE TO BE DISPLAYED
Flop
• else if(rating <= 3.4F)
• 2.1 to 3.4 Semi-hit
• 3.5 to 4.5 Hit
• message = "Semi-hit";


4.6 to 5.0 Super Hit
Write a main method to create an object of the
• System.out.print("Enter the title of the movie : ");
class and call the above me*/
• this.title = sc.nextLine();
• else if(rating <= 4.5F)
• import java.uil.*;
• message = "Hit";
• class movieMagic

• {
• System.out.print("Enter the rating of the movie: "); • else
• // Instance variables • this.rating = Float.parseFloat(sc.nextLine()); • message = "Super Hit";
• int year; • // check and ensure minimum value is 0.0 • System.out.println(this.title + " [" + this.year + "] is a " +
• String title; • if(this.rating < 0.0F) message);
• float rating; • { • }
• this.rating = 0.0F; • public static void main(String[] args)
• // Constructor
• } • {
• public movieMagic()
• // check and ensure maximum value is 5.0 • movieMagic movie = new movieMagic();
• {
• if(this.rating > 5.0F) • movie.accept();
• this.year = 0;
• {
• this.title = ""; • movie.display();
• this.rating = 0.0F; • this.rating = 5.0F;
• }
• } • } }
• }
• Q4. Define a class called FruitJuice with the • // input and store the member details // utility method to ask user to enter a String

following description:
• public void input()

Instance variables/data members:
• int product_code – stores the product code • { // and return the String value entered
number
• String flavour – stores the flavor of the juice. • product_code = askInt("Enter the product code: ");

(orangle, apple, etc.)
String pack_type – stores the type of packaging • flavour = askString("Enter the flavour: "); • public String askString(String prompt)
(tetra-pack, bottle, etc.)
• pack_type = askString("Enter the pack type: ");
• int pack_size – stores package size (200ml,
400ml, etc.) • pack_size = askInt("Enter the pack size: "); • {
• int product_price – stores the price of the
• product_price = askInt("Enter the product price: ");

product
Member methods:
• }
• Scanner sc = new Scanner( System.in );
• FruitJuice() – default constructor to initialize


integer data members to zero and string data
members to “” System.out.print(prompt);
• void input() – to input and store the product • // compute the discount
code, flavor, pack type, pack size and product
price • public void discount() • String s = sc.nextLine();
• void discount() – to reduce the product price by
10
• {
• void display() – to display the product code,
flavor, pack type, pack size and product price
• product_price -= 10; • return s;
• }
• import java.util.*; • }
• class FruitJuice • // display the object details
• { • public void display()
• // instance variables / • {
data members • System.out.println("Product code : " + product_code); • // main method to execute the class
• int product_code; • System.out.println("Flavour : " + flavour);
• String flavour; • System.out.println("Pack type : " + pack_type); • public static void main(String[] args)
• String pack_type; • System.out.println("Pack size : " + pack_size + " ml.");
• int pack_size, • System.out.println("Product price: " + product_price + " • {
product_price; Rs.");
• System.out.println(); • FruitJuice obj = new FruitJuice();
• // zero-argument • }
constructor • // utility method to ask user to enter an int
• obj.input(); // (1) ask user data for one juice
• public FruitJuice() • // and return the int value entered • obj.display(); // (2) display
• { • public int askInt(String prompt)
• product_code = 0; • { • obj.discount(); // (3) compute discount
• flavour = ""; • Scanner sc = new Scanner( System.in );
• pack_type = ""; • System.out.print(prompt); • obj.display(); // (4) display discounted price
• pack_size = 0; • int n = sc.nextInt();
• product_price = 0; • String temp = sc.nextLine(); // read the end of line • }
• } • return n;
• Q4. Define a class called Library with the following description: // compute and display the fine charged // main method
• Instance variables/data members:
• int acc_num – stores the accession number of the book
• String title – stores the title of the book public void compute(int nDays) public static void main(String[] args)
• String author – stores the name of the author
• Member Methods:
• void input() – to input and store the accession number, title and author { { Library book1 = new Library();
• void compute() – to accept the number of days late, calculate and display the fine charged at
the rate of Rs. 2 per day
• void display() – to display the details in following format int fine = nDays * 2; Library book2 = new Library();
• Accession Number Title Author


---------------- ----- ------
Write a main method to create an object of the class and call the above member methods.
System.out.println("Fine charged: " + fine); book1.input();

import java.util.*; } book2.input();


class Library
{ // instance variables, data members
// display the info book1.compute(5);
int acc_num; public void display() book2.compute(7);
String title, author;
{ printInt(acc_num, 16+1); // print acc_num in width of 16 Library.displayHeader();
// zero argument constructor
printStr(title, 30+1); // print title in width of 30 book1.display();
public Library()
{ printStr(author, 20); // print author in width of 20 book2.display();
acc_num = 0;
title = ""; System.out.println(); }
author = ""; } // print argument1 in a field of width argument2
}
// static method to print the header once only public static void printInt(int n, int width)
// input
public static void displayHeader() { String s = "" + n;
public void input()

{ Scanner sc = new Scanner( System.in );


{ printStr("Accession Number", 16+1); printStr(s, width); // call the String method

String temp = ""; printStr("Title", 30+1); }

System.out.print("Enter the accession number: "); printStr("Author", 20); // print argument1 in a field of width argument2
acc_num = sc.nextInt(); System.out.println(); public static void printStr(String s, int width)
temp = sc.nextLine(); // for the trailing newline printStr("----------------", 16+1); { System.out.print(s);
System.out.print("Enter the title: ");
printStr("------------------------------", 30+1); // print spaces to fill up the field width
title = sc.nextLine();
printStr("--------------------", 20); for(int i=s.length()+1; i<=width; i++)
System.out.print("Enter the author: ");
System.out.println(); System.out.print(" "); }}
author = sc.nextLine(); }
• Q5. Define a class student as described below: • // Accept the student details // display the details
• Data members/instance variables: • // This is static and will use new student()
• name, age, m1, m2, m3 (marks in three subjects), maximum, public void display()
• // to create the student object and return the
average
same { System.out.println("Student details");
• Member methods:
• (i) A parameterized constructor to initialize the data members • public static student accept() System.out.print("Name: " + name);
• (ii) To accept the details of a student • {
• (iii) To compute the average and maximum out of three marks • String name; System.out.println(" Age: " + age);
• (iv) To display the name, age, marks in three subjects, maximum • int age, m1, m2, m3;
and average. System.out.println("Marks: " + m1 + ", " +
• Scanner sc = new Scanner(System.in); m2 + ", " + m3);
• Write a main method to create an object of the class and call
the above member methods. • System.out.print("Enter the name: ");
• name = sc.nextLine(); System.out.println("Maximum: " +
• import java.util.*; • System.out.print("Enter the age and 3 marks: "); maximum + ", Average: " + average);
• class student • age = sc.nextInt(); }
• { • m1 = sc.nextInt();
• // data members / instance variables • m2 = sc.nextInt();
• String name; • m3 = sc.nextInt(); // entry point main method
• int age, m1, m2, m3; • // create the object
• int maximum; • student s = new student(name, age, m1, m2, public static void main(String[] args)
• double average; m3); {
• // return the created object
• // parameterized constructor • return s; // call the static method accept() which
• will
public student(String name, int age, int m1, int m2, • }
int m3) // ask the user to enter the student details.
// compute the maximum and average
• {
• this.name = name; // Then, it will create an object of the student
public void compute()
//class and return the same.
• this.age = age;
{
• this.m1 = m1; student s1 = student.accept(); // static
• this.m2 = m2; maximum = Math.max( Math.max(m1,m2), m3 ); method
• this.m3 = m3; // Call the other methods
average = (m1 + m2 + m3) / 3.0D;
• this.maximum = 0;
• this.average = 0; } s1.compute();
• import java.io.*; • /*compute the gross salary, net salary and net • //entry point main method
• public class EmpSalary salary afterinvome tax */
• { • void calc() • public static void main(String args [])
• //instance variables • { • { BufferedReader br = new BufferedReader(new
• int empno; • double da = basic*0.1; InputStreamReader(System.in));
• String empname,empdesign; • double hra = basic*0.15; • System.out.println("Enter Employee No,
• double basic,salary; • double salary = basic + da + hra; Name,Designations:");
• • double pf = 0.8*salary; • int eno = Integer.parseInt(br.readLine());
• //default constructor • double net = salary-pf; • String ename = br.readLine();
• EmpSalary() • System.out.println("DA : "+da+"\tHRA : • String edesig = br.readLine();
• { "+hra+"\tSalary : "+salary); • EmpSalary emp1 = new EmpSalary(eno, ename,
• empno = 0; • edesig,200);
• empname = ""; • double it = 0.02*salary; • EmpSalary emp1 = new
• empdesign = ""; • double net_sal_it = salary - (pf + it); EmpSalary(2345,"Swati","Accountant",2000);
• basic = 0.0; • System.out.println("PF = : "+"\tRs. "+pf); • emp1.display();
• salary = 0.0; • System.out.println("Income Tax = : "+"\tRs. • emp1.calc();
• } "+it); • EmpSalary emp2 = new
• • System.out.println("Net salary after income tax EmpSalary(6423,"Flight","Pilot", 4000);
• //parameterized constructor = : "+"\tRs. "+net_sal_it); • emp2.display();
• EmpSalary (int no,String name,String • } • emp2.calc();
empdesign,float bas) • • }
• { • //print the salary slip •
• empno = no; • void display() • }
• empname = name; • {
• this.empdesign = empdesign; • System.out.println("\t\tSalary Slip");
• basic = bas; • System.out.println("Employee no : "+empno);
• salary = 0.0; • System.out.println("Employee name :
• } "+empname);
• System.out.println("Employee designation :
"+empdesign);
• }

Vous aimerez peut-être aussi