Q1 Write a program to initialize a variable of type byte to 1 and then successively multiply it by 2 and display its value 8 times.Explain reason for last result?
Ans.
Output :
2=>
4=>
8=>
16=>
32=>
64=>
-128=>
0=>
This is because byte ranges from -128 to 127.Now when the value in second last loop becomes 128 i.e. out of range for byte it shows corresponding in range value after 127 i.e. -128.Now next value i.e. 256 will be 0 since adding 128 to -128 goes to 0
-----------------------------------------------------------------------------
-128 0 127(next value again starts from -128)
This gap is of 128
Q2 WAP to declare and initialize a double variable with some value such as 1234.5678
Retrieve the integral part of the value and store it in a variable of type long,and retrieve the first 4 digits of the fractional part and store them in an integer of type short.Display the value of double variable by outputting the 2 values stored as integers.
1234 5678
Q3 WAP that defines a floating point variable initialized with a dollar value of your income and a second floating point variable initialized with a value corresponding to a tax rate of 35 percent.Calculate and output the amount of tax you must pay with dollers and cents stored as separate integer values(use 2 variables of type int to hold tax perhaps taxDollars and taxCents)
Output:
Tax Due Dollers 159 Cents 25
Q4 The diameter of sun is approximately 865,000 miles.The diameter of earth is approximately 7600 miles.Use the methods in the class math to calculate following
Output :
Volume of Sun is 2.54160588899484096E17
Volume of Earth is 1.723854720877791E11
Volume of Sun to Volume of Earth is 1474373.5990122468
Q5 Write a program to display a random choice from a set of six choices for breakfast (you could use any
set — for example, scrambled eggs, waffl es, fruit, cereal, toast, or yogurt).
Ans:
Q6 When testing whether an integer is a prime, it is sufficient to try to divide by integers up to and including
the square root of the number being tested. Rewrite the program example from this chapter to use this
approach.
Q7 A lottery requires that you select six diff erent numbers from the integers 1 to 49. Write a program to do this for you and generate fi ve sets of entries.
Array Contains 38 so skipping
Array Contains 15 so skipping
Array Contains 24 so skipping
Array Contains 27 so skipping
Array Contains 36 so skipping
Array Contains 36 so skipping
Array Contains 39 so skipping
Array Contains 41 so skipping
Array Contains 37 so skipping
Array Contains 36 so skipping
Array Contains 41 so skipping
Array Contains 46 so skipping
Array Contains 29 so skipping
Array Contains 17 so skipping
Array Contains 7 so skipping
Array Contains 32 so skipping
Array Contains 4 so skipping
Array Contains 34 so skipping
Array Contains 31 so skipping
15
45
27
16
37
32
24
39
10
19
38
26
25
41
36
46
48
29
20
17
4
7
12
34
5
3
31
11
0
2
Q8 Write a program to generate a random sequence of capital letters that does not include vowels.
T Character already exists
V Character already exists
T
V
S
B
P
Ans.
public class Testc1q1 {
public static void main(String args[])
{
byte v1=1;
for(int i=1;i<=8;i++)
{
v1=(byte)(v1*2);
System.out.println(v1+"=>");
}
}
}
Output :
2=>
4=>
8=>
16=>
32=>
64=>
-128=>
0=>
This is because byte ranges from -128 to 127.Now when the value in second last loop becomes 128 i.e. out of range for byte it shows corresponding in range value after 127 i.e. -128.Now next value i.e. 256 will be 0 since adding 128 to -128 goes to 0
-----------------------------------------------------------------------------
-128 0 127(next value again starts from -128)
This gap is of 128
Q2 WAP to declare and initialize a double variable with some value such as 1234.5678
Retrieve the integral part of the value and store it in a variable of type long,and retrieve the first 4 digits of the fractional part and store them in an integer of type short.Display the value of double variable by outputting the 2 values stored as integers.
public class Testc1q2 {
public static void main(String args[])
{
double v1=1234.5678;
long ipart=(long)v1;
double rem_part=v1-ipart;
short fpart=(short)(rem_part*10000);
System.out.println(ipart+" "+fpart);
}
}
Output :1234 5678
Q3 WAP that defines a floating point variable initialized with a dollar value of your income and a second floating point variable initialized with a value corresponding to a tax rate of 35 percent.Calculate and output the amount of tax you must pay with dollers and cents stored as separate integer values(use 2 variables of type int to hold tax perhaps taxDollars and taxCents)
public class Testc1q3 {
public static void main(String args[])
{
float income=455;
float taxrate=35;
int taxDollers,taxCents;
taxDollers=(int)(income*(taxrate/100));
taxCents=(int)((income*(taxrate/100)-taxDollers)*100);
System.out.println("Tax Due Dollers "+taxDollers+" Cents "+taxCents);
}
}
Output:
Tax Due Dollers 159 Cents 25
Q4 The diameter of sun is approximately 865,000 miles.The diameter of earth is approximately 7600 miles.Use the methods in the class math to calculate following
- The volume of earth in cubic miles
- The volume of sun in cubic miles
- The ratio of volume of sun to the volume of earth
import java.lang.Math.*;
public class Testc1q4 {
public static void main(String args[])
{
double dSun=865000;
double dEarth=7600;
double rSun=(double)(dSun/2);
double rEarth=(double)(dEarth/2);
double vSun,vEarth;
vSun = (4 / 3)*Math.PI * Math.pow(rSun,3);
vEarth = (4 / 3)*Math.PI * Math.pow(rEarth,3);
double VEarthSun=(double)(vSun/vEarth);
System.out.println("Volume of Sun is "+vSun);
System.out.println("Volume of Earth is "+vEarth);
System.out.println("Volume of Sun to Volume of Earth is "+ (VEarthSun));
}
}
Output :
Volume of Sun is 2.54160588899484096E17
Volume of Earth is 1.723854720877791E11
Volume of Sun to Volume of Earth is 1474373.5990122468
Q5 Write a program to display a random choice from a set of six choices for breakfast (you could use any
set — for example, scrambled eggs, waffl es, fruit, cereal, toast, or yogurt).
Ans:
public class c3q1 {
public static void main(String args[])
{
Random rm=new Random();
int i=rm.nextInt()%6;
int random=i+1;
switch(random)
{
case 1:
System.out.println("Scrambled Eggs");
break;
case 2:
System.out.println("Waffles");
break;
case 3:
System.out.println("Fruit");
break;
case 4:
System.out.println("Cereal");
break;
case 5:
System.out.println("Toast");
break;
case 6:
System.out.println("Yogurt");
break;
default:
System.out.println("Miss!!!");
}
}
}
Q6 When testing whether an integer is a prime, it is sufficient to try to divide by integers up to and including
the square root of the number being tested. Rewrite the program example from this chapter to use this
approach.
public class prime {
public static void main(String args[])
{
int limit=20;
for(int i=2;i<limit;i++)
{
int isPrime=1;
for(int j=2;j<=Math.sqrt(i);++j)
{
if(i%j==0)
{
isPrime=0;
continue;
}
}
if(isPrime==1)
{
System.out.println(i+",");
}
}
}
}
Q7 A lottery requires that you select six diff erent numbers from the integers 1 to 49. Write a program to do this for you and generate fi ve sets of entries.
import java.util.Arrays;
import java.util.Random;
public class c3q3
{
public static void main(String args[])
{
Random rn=new Random();
Integer lottery_numbers[]=new Integer[30];
for(int i=0;i<30;)
{
int gen=rn.nextInt()%49;
int randomnum;
randomnum=Math.abs(gen+1);
if(Arrays.asList(lottery_numbers).contains(randomnum))
{
System.out.println("Array Contains "+randomnum+" so skipping");
}
else
{
lottery_numbers[i]=randomnum;
i++;
}
}
for(int i=0;i<lottery_numbers.length;i++)
{
System.out.println(lottery_numbers[i]+" ");
}
}
}
Output :Array Contains 38 so skipping
Array Contains 15 so skipping
Array Contains 24 so skipping
Array Contains 27 so skipping
Array Contains 36 so skipping
Array Contains 36 so skipping
Array Contains 39 so skipping
Array Contains 41 so skipping
Array Contains 37 so skipping
Array Contains 36 so skipping
Array Contains 41 so skipping
Array Contains 46 so skipping
Array Contains 29 so skipping
Array Contains 17 so skipping
Array Contains 7 so skipping
Array Contains 32 so skipping
Array Contains 4 so skipping
Array Contains 34 so skipping
Array Contains 31 so skipping
15
45
27
16
37
32
24
39
10
19
38
26
25
41
36
46
48
29
20
17
4
7
12
34
5
3
31
11
0
2
Q8 Write a program to generate a random sequence of capital letters that does not include vowels.
import java.util.Arrays;
import java.util.Random;
public class c3q4 {
public static void main(String args[])
{
char symbol='A';
Character[] symbols=new Character[5];
Random rand=new Random();
int RandomNum;
for(int i=0;i<symbols.length;)
{
RandomNum=rand.nextInt((90-65)+1)+65;
symbol=(char)(RandomNum);
if(Arrays.asList(symbols).contains(symbol))
{
System.out.println(symbol+" Character already exists");
}
else
{
symbols[i]=symbol;
i++;
}
}
for(int i=0;i<symbols.length;i++)
{
System.out.println(symbols[i]);
}
}
}
Output:T Character already exists
V Character already exists
T
V
S
B
P
No comments:
Post a Comment