IcSe 2014 PrOgRaMmInG SoLuTiOn
Question 4.
Define a class named 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)
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:
(i) movieMagic() Default constructor to initialize numeric data members to 0 and String data member to “”.
(ii) void accept() To input and store year, title and rating.
(iii) void display() To display the title of a movie and a message based on the rating as per the table below.
Rating | Message to be displayed |
0.0 to 2.0 | Flop |
2.1 to 3.4 | Semi-hit |
3.5 to 4.5 | Hit |
4.6 to 5.0 | Super Hit |
Write a main method to create an object of the class and call the above member methods
Solution :
import java.util.Scanner;
class movieMagic
{
Scanner S = new Scanner(System.in);
Scanner StringScanner = new Scanner(System.in);
int year;
String title;
float rating;
movieMagic()
{
year = 0;
rating = 0.0f;
title = "";
}
void accept()
{
System.out.println("Enter the
name, year and rating of the movie.");
title = StringScanner.nextLine();
year = S.nextInt();
rating = S.nextFloat();
}
void display()
{
System.out.print("Title
:"+title+"\nYear :"+year+"\nRating
:"+rating+"\nMessage : Movie was a ");
if(rating<=2.0)
System.out.print("Flop");
if((rating>2.0)&&(rating<=3.4))
System.out.print("Semi-Hit");
if((rating>3.4)&&(rating<=4.5))
System.out.print("Hit");
if((rating>4.5)&&(rating<=5.0))
System.out.print("Super
Hit");
}
void main()
{
accept();
display();
}
}
Question 5.
A special two-digit number is such that when the sum of its digits is added to the product of its digits, the result is equal to the original two-digit number.
Example: Consider the number 59.
Sum of digits = 5 + 9 = 14
Product of its digits = 5 x 9 = 45
Sum of the sum of digits and product of digits= 14 + 45 = 59
Sum of digits = 5 + 9 = 14
Product of its digits = 5 x 9 = 45
Sum of the sum of digits and product of digits= 14 + 45 = 59
Write a program to accept a two-digit number. Add the sum of its digits to the product of its digits. If the value is equal to the number input, output the message “Special 2-digit number” otherwise, output the message “Not a Special 2-digit number”.
Solution :
import java.util.Scanner;
class Question5
{
Scanner S = new Scanner(System.in);
public boolean isSpecial(int x)
{
int prod = 1;
int r = x;
int sum = 0;
while(x!=0)
{
int d = x%10;
prod = prod*d;
sum = sum +d;
x=x/10;
}
if((prod+sum)==r)
return true;
else
return false;
}
void main()
{
System.out.println("Enter the
number");
int no = S.nextInt();
if(isSpecial(no))
System.out.println("It is a
Special No.");
else
System.out.println("Not a
Special No.");
}
}
Write a program to assign a full path and file name as given below. Using library functions, extract and output the file path, file name and file extension separately as shown.
Input: C:Users\admin\Pictures\flowers.jpg
Output:
Path: C:Users\admin\Pictures
File name: flowers
Extension: jpg
Output:
Path: C:Users\admin\Pictures
File name: flowers
Extension: jpg
Solution :
import
java.util.Scanner;
class
Question6
{
Scanner S = new Scanner(System.in);
String input,path,extension,filename,temp;
void method1(String str)
{
path = str.substring(0,(str.lastIndexOf('\\')));
extension = str.substring((str.lastIndexOf('.')));
filename =
str.substring((str.lastIndexOf('\\')+1),(str.lastIndexOf('.')));
System.out.println("Path : "+path+"\nFile
name: "+filename+"\nExtension: "+extension);
}
void main()
{
System.out.println("Enter the String ");
input = S.nextLine();
method1(input);
}
}
Question 7.
Design
a class to overload a function area() as
follows:
(i) double area(double a, double b, double
c) with three double arguments, returns the area of a scalene triangle using
the formula: area = √(s — a)(s- b)(s - c)
Where s = (a+b+c)/2
(ii) double area(int a, int b, int height) with
three integer arguments, returns the area of a trapezium using the
formula:
area = 0.5 X heightx(a+b)
(ii) double area(double diagonal 1, double
diagonal 2) with two double arguments, returns
the area of a rhombus using the formula:
Area =
0.5X(diagonal1 x diagonal2) [15]
import java.util.Scanner;
class Question6
{
Scanner S =
new Scanner(System.in);
String
input,path,extension,filename,temp;
void
method1(String str)
{
path = str.substring(0,(str.lastIndexOf('\\')));
extension = str.substring((str.lastIndexOf('.')));
filename = str.substring((str.lastIndexOf('\\')+1),(str.lastIndexOf('.')));
System.out.println("Path : "+path+"\nFile name:
"+filename+"\nExtension: "+extension);
}
void main()
{
System.out.println("Enter the String ");
input = S.nextLine();
method1(input);
}
}
Using the
switch statement, write a menu driven program to calculate the maturity amount
of a Bank Deposit. The user is given the following options:
(i) Term
Deposit
(ii)
Recurring Deposit
For option :-
(i) accept
principal(P), rate of interest(r) and time period in years(n). Calculate
Amount
and output the maturity amount (A) receivable using the formula A.= P[1 +(i/100)]
(ii) accept
Monthly Installment (P), rate of interest(r) and time period in months (n).
Calculate and output the maturity amount(A) receivable using the formula A=(Pxn)+((Pxnx(n+1)xr)/ (2x100x12) For an incorrect option, an appropriate
error message should be displayed. [15]
import
java.util.Scanner;
class Question8
{
Scanner S = new Scanner(System.in);
void RecurringDeposit()
{
System.out.println("Enter the
Monthly Deposit");
double P = S.nextDouble();
System.out.println("Enter the Rate
of Interest");
double r = S.nextDouble();
System.out.println("Enter the time
in months");
int n = S.nextInt();
double A = (P*n) +
((P*n*(n+1)*r))/(100*12*2);
System.out.println("Maturity
Amount : "+A);
}
void TermDeposit()
{
System.out.println("Enter the
Principal Ammount");
double P = S.nextDouble();
System.out.println("Enter the Rate
of Interest");
double r = S.nextDouble();
System.out.println("Enter the time
in years");
int n = S.nextInt();
double A = Math.pow(((1+(r/100))),n);
A = A*P;
System.out.println("Maturity
Amount : "+A);
}
void main()
{
System.out.println("Press 1 for
Term Deposit\nPress 2 for Recurring Deposit\nPress 3 to exit");
int ch = S.nextInt();
switch(ch)
{
case 1 : TermDeposit();
break;
case 2 : RecurringDeposit();
break;
case 3 : System.exit(0);
break;
default :
System.out.println("Wrong choice Entered!");
System.exit(0);
}
}
}
Question 9.
Write a program to accept the year of graduation from school as an integer
value from the user. Using the Binary search technique on the sorted array of
Integers given below, output the message "Record exists" if the value
input is located in the array. If not, output the message "Record does not
exist". { 1982, 1987, 1993, 1996, 1999, 2003, 2006, 2007, 2009, 2010) [15]
import
java.util.Scanner;
class Question9
{
Scanner S = new Scanner(System.in);
public boolean BinarySearch(int Array[],int
search)//this Binary Search Method can be used in any question in Integer
Arrays[].
{
int len = Array.length;
boolean flag = false;
int
lowerIndex=0,higherIndex=len-1,middleIndex;
while(lowerIndex<=higherIndex)
{
middleIndex=(lowerIndex+higherIndex)/2;
if (search==Array[middleIndex])
{
System.out.println("Element
"+search+" found at "+(middleIndex+1)+" position");
flag= true;
break;
}
else
if(search<Array[middleIndex])
higherIndex=middleIndex-1;
else
lowerIndex=middleIndex+1;
}
return flag;
}
void main()
{
System.out.println("Enter the year
to be searched");
int syear = S.nextInt();
int year[] = {1982, 1987, 1993, 1996,
1999, 2003, 2006, 2007, 2009, 2010};
if(BinarySearch(year,syear))
System.out.println("Record
Exists");
else
System.out.println("Record does
not Exist");
}
}
Download the printable PDF file >>HERE<<
----------------x---------------------x----------------x---------------------x----------------x---------------------x----------------x---------------------x