JAVA CONSTRUCTORS PROGRAM, Summaries of Computer science

THIS IS USEFUL FOR GRADE 10 ICSE STUDENTS. PROGRAMS BASED ON CONSTRUCTORS ARE GIVEN HERE

Typology: Summaries

2023/2024

Available from 06/25/2024

scoremaster-icse-java
scoremaster-icse-java 🇮🇳

4 documents

1 / 10

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Question 1:
Define a class taximeter having the following description:
Data members/instance variables
int taxino - to store taxi number
String name - to store passenger's name
int km - to store number of kilometres travelled
Member functions:
taximeter() -- constructor to initialize taxino to 0, name to "" and km to 0.
input() - to store taxino, name, km
calculate() - to calculate bill for a customer according to given conditions
kilometers travelled(km) Rate/km
Less than 1 km: Rs 25
1 km to 6 km: Rs 20
Greater than 6 km and upto 12 km: Rs 18
Greater than 12 km and upto 18 km: Rs 15
Greater than 18 km: Rs 10
display()- To display the details in the following format
Taxino Name Kilometres travelled Bill amount
Program Code :
import java.io.*;
class taximeter
{
int taxino,km;
String name;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
taximeter()
{
taxino=0;
km=0;
name="";
}
public void input()throws Exception
{
System.out.println("Enter the name:");
name=br.readLine();
System.out.println("Enter the taxi number:");
taxino=Integer.parseInt(br.readLine());
System.out.println("Enter the km travelled:");
km=Integer.parseInt(br.readLine());
}
public void calculate()
{
System.out.println("Taxino Name Kilometres travelled Bill amount ");
System.out.print(taxino);
System.out.print(" "+name);
System.out.print(" "+km);
if(km<1 o:p="o:p">
System.out.print(" Rs. 25");
else if(km>=1 && km<=6)
System.out.print(" Rs."+(20*km));
else if(km>6 && km<=12)
System.out.print(" Rs."+(18*km));
else if(km>12 && km<=18)
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download JAVA CONSTRUCTORS PROGRAM and more Summaries Computer science in PDF only on Docsity!

Question 1:

Define a class taximeter having the following description: Data members/instance variables int taxino - to store taxi number String name - to store passenger's name int km - to store number of kilometres travelled Member functions: taximeter() -- constructor to initialize taxino to 0, name to "" and km to 0. input() - to store taxino, name, km calculate() - to calculate bill for a customer according to given conditions kilometers travelled(km) Rate/km Less than 1 km: Rs 25 1 km to 6 km: Rs 20 Greater than 6 km and upto 12 km: Rs 18 Greater than 12 km and upto 18 km: Rs 15 Greater than 18 km: Rs 10 display()- To display the details in the following format Taxino Name Kilometres travelled Bill amount

Program Code :

import java.io.; class taximeter { int taxino,km; String name; BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); taximeter() { taxino=0; km=0; name=""; } public void input()throws Exception { System.out.println("Enter the name:"); name=br.readLine(); System.out.println("Enter the taxi number:"); taxino=Integer.parseInt(br.readLine()); System.out.println("Enter the km travelled:"); km=Integer.parseInt(br.readLine()); } public void calculate() { System.out.println("Taxino Name Kilometres travelled Bill amount "); System.out.print(taxino); System.out.print(" "+name); System.out.print(" "+km); if(km<1 o:p="o:p"> System.out.print(" Rs. 25"); else if(km>=1 && km<=6) System.out.print(" Rs."+(20km)); else if(km>6 && km<=12) System.out.print(" Rs."+(18*km)); else if(km>12 && km<=18)

System.out.print(" Rs."+(15km)); else if(km>18 ) System.out.print(" Rs."+(10km)); } public static void main(String args[])throws Exception { taximeter ob=new taximeter(); ob.input(); ob.calculate(); } }

else

charge=500*days;

days=0;

if(days>5)

charge=charge+400*5;

days=days-5;

else if(days>0)

charge=400*days;

days=0;

if(days>0)

charge=charge+200*days;

display();

void display()

System.out.println("Bike No.\t\tPhoneNo.\t\tNo. of days\t\tCharge");

System.out.println(bno+"\t\t\t\t"+phno+"\t\t\t\t"+days+"\t\t\t\t"+charge);

public static void main(String args[])throws Exception

mobike ob=new mobike();

ob.input();

Question 3:

Define a class movieMagic with the following description:

Instance variables/data members:

int year: to store the year of release of a movie

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:

1. movieMagic( ): Default constructor to initialize numeric data members to 0 and String data

member to “”.

2. void accept( ): To input and store year title and rating.

3. void display( ): To display the title of a movie and a message based on the rating as per the

table.

Program Code:

import java.util.; class movieMagic{ int year; String title; float rating; public movieMagic(){ year = 0; title = ""; rating = 0; } public void accept(){ Scanner sc = new Scanner( System.in ); System.out.print("Enter title: "); title = sc.nextLine(); System.out.print("Enter year: "); year = sc.nextInt(); System.out.print("Enter rating: "); rating = sc.nextFloat(); } public void display(){ String message = ""; System.out.print("Title: "+title); if( rating >= 0.0f && rating <= 2.0 ) message="Flop"; else if( rating >= 2.1f && rating <= 3.4 ) message="Semi-hit"; else if( rating >= 3.5f && rating <= 4.5 ) message="Hit"; else if( rating >= 0.0f && rating <= 2.0 ) message="Super Hit"; System.out.println("Result: "+ message ); } public static void main( String args[] ){ movieMagic obj = new movieMagic( ); obj.accept( ); obj.display( ); } }*

price = Double.parseDouble(br.readLine()); } void calculate() // calculate method { if(price <= 1000) { discount = price * 0.02; // 2% discount price } else if(price > 1000 && price <= 3000) // 10% discount price { discount = price * 0.1; } else { discount = price *.15; // 15% discount price } } void display() // display method { price = price - discount; System.out.println("\n\n Book Name :" + Bname); System.out.println("\n Price after discount :" + price); } public static void main(String args[]) throws IOException { // Object creation BookFair ob = new BookFair(); ob.input(); ob.calculate(); ob.display(); } }

Question 5:

import java.util.Scanner;

class Student

String name;

int age;

int m1, m2, m3;

int maximum;

double average;

Student(String n, int a, int marks1, int marks2, int marks3, int max, double avg) {

name = n;

age = a;

m1 = marks1;

m2 = marks2;

m3 = marks3;

maximum = max;

average = avg;

void accept()

Scanner scanner = new Scanner(System.in);

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

name = scanner.next();

System.out.print("Enter age: ");

age = scanner.nextInt();

System.out.print("Enter marks1: ");

m1 = scanner.nextInt();

System.out.print("Enter marks2: ");

m2 = scanner.nextInt();

System.out.print("Enter marks3: ");

m3 = scanner.nextInt();

void compute()

average = (m1 + m2 + m3) / 3.0;

maximum = Math.max(m1, (Math.max(m2, m3)));

public void display()

System.out.println("Name: " + name);

Question 6:

Define a class employee having the following description:

Data members

int pan to store personal account number

Instance variables

String name to store name

double taxincome to store annual taxable income

double tax to store tax that is calculated

Member functions:

input( ) Store the pan number, name, taxable income

calc( ) Calculate tax for an employee

display( ) Output details of an employee

Write a program to compute the tax according to the given conditions and display the output as per

given format.

Total Annual Taxable Income Tax Rate

Upto Rs. 1,00,000 No tax

From 1,00,001 to 1,50,000 10% of the income exceeding Rs. 1,00,

From 1,50,001 to 2,50,000 Rs. 5000 + 20% of the income exceeding Rs. 1,50,

Above Rs. 2,50,000 Rs. 25,000 + 30% of the income exceeding Rs. 2,50,

Output:

Pan Number Name Tax-income Tax

Program Code: