Question 4:
Define a class called FruitJuice with the following description:
[15]
Instance variables/data members:
int product_code – stores the product code number
String flavour – stores the flavor of the juice.(orange, apple, etc)
String pack_type – stores the type of packaging (tetra-pack, bottle etc)
int pack_size – stores package size (200ml, 400ml etc)
int product_price – stores the price of the product
Member Methods:
(i)FriuitJuice() – default constructor to initialize integer data members
to zero and string data members to “”.
(ii)void input() – to input and store the product code, flavor, pack type,
pack size and product price.
(iii)void discount() – to reduce the product price by 10.
void display() – to display the product code, flavor, pack
type,
pack size and product price.
Solution :
import
java.util.Scanner;
class
FruitJuice
{
int product_code;
String flavour;
String pack_type;
int pack_size;
int product_price;
public FruitJuice()
{
product_code = 0;
flavour = "";
pack_type = "";
pack_size = 0;
product_price = 0;
}
public void input()
{
Scanner scanner = new Scanner(System.in);
System.out.print("Enter productcode: ");
product_code = scanner.nextInt();
System.out.print("Enter flavour:");
flavour = scanner.next();
System.out.print("Enter pack type:");
pack_type = scanner.next();
System.out.print("Enter pack size:");
pack_size = scanner.nextInt();
System.out.print("Enter productprice: ");
product_price = scanner.nextInt();
}
public void discount()
{
product_price = (int) ((90/100) *product_price);
}
public void display()
{
System.out.println("Product Code:" + product_code+"\nFlavour:
"+flavour+"\nPack Type:"+pack_type+"\nPack Size:
"+pack_size+"\nProduct Price:"+product_price);
}
}
Question 5:
The International Standard Book Number (ISBN) is a unique numeric
book identifier which is printed on every book. The ISBN is based upon a
10-digit code. The ISBN is legal if:
1xdigit1 + 2xdigit2 + 3xdigit3 + 4xdigit4 + 5xdigit5 + 6xdigit6 + 7xdigit7 +
8xdigit8 + 9xdigit9 + 10xdigit10 is divisible by 11.
Example: For an ISBN 1401601499
Sum=1×1 + 2×4 + 3×0 + 4×1 + 5×6 + 6×0 + 7×1 + 8×4 + 9×9 + 10×9 = 253 which is
divisible by 11.
Write a program to:
(i) input the ISBN code as a 10-digit integer.
(ii) If the ISBN is not a 10-digit integer, output the message “Illegal ISBN”
and terminate the program.
(iii) If the number is 10-digit, extract the digits of the number and compute
the sum as explained above.
If the sum is divisible by 11, output the message, “Legal ISBN”. If the sum is
not divisible by 11, output the message, “Illegal ISBN”. [15]
Solution :
import java.util.Scanner;
class Question5
{
Scanner S =
new Scanner(System.in);
public
boolean isISBN(long x)
{
long c = x;
long sum = 0;
for(int i = 1;i<=10;i++)
{
long d = c%10;
c=c/10;
sum = sum + (d*i);
}
if((sum%11)==0)
return true;
else
return false;
}
void main()
{
System.out.println("Enter an ISBN number");
long no = S.nextLong();
if(isISBN(no))
System.out.println("Legal ISBN");
else
System.out.println("Illegal ISBN");
}
}
Question 6:
Write a program that encodes a word into Piglatin. To translate
word into Piglatin word, convert the word into uppercase and then place the
first vowel of the original word as the start of the new word along with the
remaining alphabets. The alphabets present before the vowel being shifted
towards the end followed by “AY”.
Sample Input(1): London Sample Output(1): ONDONLAY
Sample Input(2): Olympics Sample Output(2): OLYMPICSAY [15]
Solution :
import java.util.Scanner;
class Question6
{
Scanner
S = new Scanner(System.in);
String
str,pigword="",temp="";
char
c,cc;
public
boolean isVowel(char ch)
{
ch = Character.toUpperCase(ch);
if((ch=='A')||(ch=='E')||(ch=='I')||(ch=='O')||(ch=='U'))
return true;
else
return false;
}
public
void piglatin()
{
System.out.println("Enter the Word to be converted in Piglatin :-");
str = S.nextLine();
str = str.toUpperCase();
for(int i = 0;i<str.length();i++)
{
c=str.charAt(i);
if(isVowel(c))
{
pigword = str.substring(i,str.length());
pigword = pigword+temp+"AY";
break;
}
temp = temp+c;
}
System.out.println("Piglatin String : "+pigword);
}
}
Question 7:
Write a program to input 10 integer elements in an array and sort
them
In descending order using bubble sort technique. [15]
Solution :
import java.util.Scanner;
class Question7
{
Scanner
S = new Scanner(System.in);
int
lol[] = new int[10];
void
BubbleSort(int Array[])//mmethod 1 to sort elements in Ascending order.
{
int n = Array.length;
int c = 0, f = 1;
for(int i=0; (i<n-1) && (f==1); i++)
{
f = 0;
for(int j=0; j<n-1-i; j++)
{
if(Array[j] > Array[j+1])
{
c = Array[j];
Array[j] = Array[j+1];
Array[j+1] = c;
f = 1;
}
}
if(f == 0)
break;
}
System.out.println("The Array Sorted in Descending order is : ");
for(int i = (n-1);i>=0;i--)
System.out.println(Array[i]);
}
void
Bubbles(int Array[])// Method 2 to actually sort the Array in descending oreder
and printing it as it is.
{
int len = Array.length;
for(int i = 0;i<len;i++)
{
for(int j = 1;j<len;j++)
{
if(Array[j-1]<Array[j])
{
int c = Array[j];
Array[j] = Array[j-1];
Array[j-1] = c;
}
}
}
System.out.println("The Array Sorted in Descending order is : ");
for(int i = 0;i<len;i++)
System.out.println(Array[i]);
}
void
main()
{
System.out.println("Enter the array to be sorted in decending
order");
for(int i=0;i<10;i++)
lol[i] = S.nextInt();
BubbleSort(lol);
Bubbles(lol);//For this question, this method should be used as it is clearly
said to SORT the array in descending order and print it.
}
}
Question 8:
Design
a class to overload a function series() as follows: [15]
(i) double series(double n) with one double argument and
returns the sum of the series.
sum = 1/1 + 1/2 + 1/3 + ….. 1/n
(ii) double series(double a, double n) with two double arguments
and returns the sum of the series.
sum = 1/a2 + 4/a5 + 7/a8 + 10/a11... till n terms
Solution :
class Question8
{
double
series(double n)
{
double sum = 0;
for(int i = 1;i<=n;i++)
sum = sum + 1.0/i;
return sum;
}
double
series(double a, double n)
{
double sum = 0;
int k = 1;
for(int i = 1;i<=n;i++)
{
sum = sum + (k++/(Math.pow(a,(k++))));
k++;
}
return sum;
}
}
Question 9:
Using the switch statement, write a menu driven program: [15]
(i) To check and display whether a number input by the user is
a composite number or not (A number is said to be a composite, if it
has one or more then one factors excluding 1 and the number itself).
Example: 4, 6, 8, 9…
(ii) To find the smallest digit of an integer that is input:
Sample input: 6524
Sample output: Smallest digit is 2
For an incorrect choice, an appropriate error message should be displayed.
Solution :
import java.util.Scanner;
class Question9
{
Scanner
S = new Scanner(System.in);
int
no,ch;
void
main()
{
System.out.println("Press 1 to check for Composite Number.");
System.out.println("Press 2 to find smallest digit in the Input.");
ch = S.nextInt();
switch(ch)
{
case 1: Prime();
break;
case 2: Dig();
break;
default: System.out.println("Wrong choice entered");
}
}
void
Dig()//My method using Arrays[]. I prefer this method because we can perform
various actions and alterations in the array containing the digits of number
easily...Useful for higher studies.
{
System.out.println("Enter the Number");
no = S.nextInt();
int copy = no;
int len = Integer.toString(no).length();//finding the number of digits in the
input.
int digits[] = new int[len];
int j = 0;
int min = 9;
while(copy>0)//extracting digits and puting them in array.
{
int d = copy%10;
copy = copy/10;
digits[j] = d;
j++;
}
for(int i =0;i<len;i++)
{
if(digits[i]<min)
min = digits[i];
}
System.out.println("Smallest digit is = "+min);
}
void
Prime()
{
System.out.println("Enter the Number to be checked");
no = S.nextInt();
if(isPrime(no))
System.out.println("Not a Composite Number");
else
System.out.println("It is a Composite Number");
}
public
boolean isPrime(int x)
{
int killme = 0;
for(int i = 2;i<x;i++)
{
if(x%i==0)
killme++;
else
continue;
}
if(killme>0)
return false;
else
return true;
}
void
Digits()
{
System.out.println("Enter the Number");
no = S.nextInt();
int copy = no;
int min = 9;
while(copy>0)
{
int d = copy%10;
copy = copy/10;
if(d<min)
min = d;
}
System.out.println("Smallest digit is = "+min);
}
}
Download the printable PDF file >>HERE<<
----------------x---------------------x----------------x---------------------x----------------x---------------------x----------------x---------------------x