0% found this document useful (0 votes)
55 views13 pages

Oop Assig 1

The document contains 6 programming questions involving Java code to perform various calculations. The code snippets include classes to calculate square roots using the Babylonian method, simulate craps games, estimate child height based on parent heights, calculate future costs accounting for inflation, calculate loan payoff amounts over time, and model population growth of green crud over time. The questions involve running the provided code and analyzing the outputs.

Uploaded by

Hadiqa Shahid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views13 pages

Oop Assig 1

The document contains 6 programming questions involving Java code to perform various calculations. The code snippets include classes to calculate square roots using the Babylonian method, simulate craps games, estimate child height based on parent heights, calculate future costs accounting for inflation, calculate loan payoff amounts over time, and model population growth of green crud over time. The questions involve running the provided code and analyzing the outputs.

Uploaded by

Hadiqa Shahid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

QUESTION NO 1:

import java.util.*;
class Babylonia
{
double n;
double guess;
double r;
double previousGuess;
double diff;

public void data_input()


{
Scanner input=new Scanner(System.in);
System.out.println("This program estimates square roots");
System.out.println("Enter a value for n");
n = input.nextDouble();

}
public void Square_root()
{
guess = n / 2;
do
{
r = n / guess;
previousGuess = guess;
guess = (guess + r) / 2;
diff = (previousGuess - guess) / ( previousGuess);
}
while(diff >= 0.01);
//for double to two decimal places
System.out.printf("The square root of %.2f is approximately: %.2f\n", n, guess);
}
}
public class Result_Babylonia
{
public static void main(String a[])
{
System.out.println("This is the main class");
Babylonia obj1=new Babylonia();
obj1.data_input();
obj1.Square_root();
}
}

QUESTION N02:
import java.text.DecimalFormat;
import java.util.Random;

public class Craps


{
public static void main(String a[ ] )
{
DecimalFormat g=new DecimalFormat("#.00%");
int dice1 = 0;
int dice2 = 0;
int sum = 0;
int point = 0;
double win=0;
double lose=0;
Random ra = new Random();
int r = ra.nextInt(6)+1;
int y = ra.nextInt(6)+1;
dice1=r;
dice2=y;

for(int i=1;i<=10000;i++){
dice1=ra.nextInt(6)+1;
dice2=ra.nextInt(6)+1;
sum=dice1+dice2;
if(sum==7 || sum==11 )
{
win++;
}
else if(sum==2||sum==3||sum==12)
{
lose++;
}
else
{
point=sum;
dice1=ra.nextInt(6)+1;
dice2=ra.nextInt(6)+1;
sum=dice1+dice2;
while(sum!=7 && sum!=point)
{
dice1=ra.nextInt(6)+1;
dice2=ra.nextInt(6)+1;
sum=dice1+dice2;
}
if(sum==7)
{
lose++;
}
else if(sum==point)
{
win++;
}
dice1=ra.nextInt(6)+1;
dice2=ra.nextInt(6)+1;
}
}
double prob=(win/(lose+win));
System.out.println("Won: "+win+" Lost:"+lose);
System.out.println("Wining Percentage: "+g.format(prob));
if(win>lose)
{
System.out.println("You Win!");
}
else
{
System.out.println("House Wins! and he will win more than you...");
}

QUESTION NO 3
import java.util.Scanner;
class HeightDemo
{
public void input()
{
String genderChild, units, userChoice;
int fatherHeight, motherHeight, childHeight;

Scanner scn = new Scanner(System.in);


while (true)
{
fatherHeight = 0;
motherHeight = 0;
childHeight = 0;
System.out.println("Enter your userChoice('boy' or 'girl'):");
genderChild = scn.next();
System.out.println("Enter 'feet' for feet and inches,or 'inch' for
inches:");
units = scn.next();
if (units.equals("feet"))
{
System.out.println("Enter father's height feet:");
fatherHeight = 12 * scn.nextInt();
System.out.println("Enter father's height inches:");
fatherHeight += scn.nextInt();
System.out.println("Enter mother's height feet:");
motherHeight = 12 * scn.nextInt();
System.out.println("Enter mother's height inches:");
motherHeight += scn.nextInt();
}
else if (units.equals("inch"))
{
System.out.println("Enter father's height inches:");
fatherHeight = scn.nextInt();
System.out.println("Enter mother's height inches:");
motherHeight = scn.nextInt();
}
if (genderChild.equals("boy"))
{
childHeight = (int) (((motherHeight * 13 /
12.0) + fatherHeight) / 2.0);
}
else if (genderChild.equals("girl"))
{
childHeight = (int) (((fatherHeight + 12
/13.0)+ fatherHeight) / 2.0);
}
if (units.equals("feet"))
{
System.out.println("The children will be"
+ " about " + childHeight / 12 + " feet, "+ childHeight % 12 + "
inches tall.");
}
else if (units.equals("inch"))
{
System.out.println("The children will be about " + childHeight +" inches
tall.");
}
System.out.println("Enter 'yes' to continue (or) 'no' to exit.");
userChoice = scn.next();
if (userChoice.equals("yes"))
System.out.println("Continuing...");
else if (userChoice.equals("no"))
break;
}
}
}
public class Result_Height
{
public static void main(String a[])
{
System.out.println("This is the main class");
HeightDemo obj1=new HeightDemo();
obj1.input();
}
}

QUESTION NO 4:

import java.util.Scanner;

class TestDemo
{
double cost ;
double inflation;
int years;
double expectedCost;

public void Data_input()


{
Scanner input = new Scanner(System.in);
System.out.print("Enter the cost :");
cost = input.nextDouble();
System.out.print("Enter the number of years:");
years = input.nextInt();
System.out.print("Enter the inflation rate:");
inflation = input.nextDouble() * 0.01;
}
public void Calculating_Cost()
{
for (int i = 0; i < years; i++)
cost += cost * inflation;
cost = (int) (cost * 100) / 100.0;
System.out.println("Item will cost about $ " + cost + " in "
+ years + " years. " );
}
}
public class Result_Cost
{
public static void main(String a[])
{
System.out.println("This is the main class");
TestDemo obj1=new TestDemo();
obj1.Data_input();
obj1.Calculating_Cost();
}
}

QUESTION N0 5:
public class InterestCalculator
{
private static final double COST_OF_STEREO_SYSTEM = 1000.00;
private static final double INTEREST_RATE_PER_YEAR = 0.18;
private static final double MONTHLY_PAYMENT = 50.00;

public static void main(String[] args)


{
double debt = COST_OF_STEREO_SYSTEM;
double interestRatePerMonth = INTEREST_RATE_PER_YEAR / 12;
double interest = 0;
double totalInterest = 0;
double principal = 0;
int numberOfMonths = 0;

while (debt > 0)


{
interest = debt * interestRatePerMonth;

if (debt >= MONTHLY_PAYMENT)


principal = MONTHLY_PAYMENT - interest;
else
principal = debt;

debt = debt - principal;

numberOfMonths++;
totalInterest += interest;
}

System.out.printf("Amount of loan: $%.2f\n",COST_OF_STEREO_SYSTEM);


System.out.printf("Interest rate per year: %.2f%%\n",
(INTEREST_RATE_PER_YEAR * 100));
System.out.printf("Monthly payment: $%.2f\n", MONTHLY_PAYMENT);
System.out.println("Number of months needed to pay off the loan: " +
numberOfMonths);
System.out.printf("Total amount of interest paid over the life of the loan: $
%.2f\n",
totalInterest);
}
}

QUESTION N0 6:
import java.util.Scanner;
class GreenCrudPopulation
{

char repeat;

public void data_input()


{
do
{
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter the initial size of a green crud
population in pounds: ");
int initialPopulation = keyboard.nextInt();
System.out.print("Enter the number of days: ");
int numberOfDays = keyboard.nextInt();
int f0 = 0;
int f1 = initialPopulation;
int f2 = f1 + f0;

for (int day = 1; day <= numberOfDays; day++)


{
if (day % 5 == 0)
{
f2 = f1 + f0;
f0 = f1;
f1 = f2;
}
}

System.out.println("The number of pounds of green crud after " +


numberOfDays + " days: " + f2);
System.out.print("Enter 'r' or 'R' to repeat: ");
repeat = keyboard.next().charAt(0);
}
while (repeat == 'R' || repeat == 'r');

}
}
public class Result_GreenCrudPopulation
{
public static void main(String a[])
{
System.out.println("This is the main class");
GreenCrudPopulation obj1=new GreenCrudPopulation();
obj1.data_input();
}
}

QUESTION N0 7:

import java.util.Scanner;
class Fact
{
public void input()
{
Scanner keyboard = new Scanner(System.in);
int x;
int n;
int k;
double fact;
double result;
char repeat;

do
{
System.out.print("Enter a value of x: ");
x = keyboard.nextInt();

result = 0;

for (n = 0; n <= 100; n++)


{
fact = 1;

for (k = 1; n > 0 && k <= n; k++)


{
fact = fact * k;
}
result += Math.pow(x, n) / fact;
if((n >= 1 && n <= 10) || n == 50 || n == 100)
{
System.out.println("At n = " + n + ", e^" + x + " = " +
result);
}
}

System.out.print("\nEnter 'y' or 'Y' to repeat: ");


repeat = keyboard.next().charAt(0);
System.out.println();
}while(repeat == 'y' || repeat == 'Y');

keyboard.close();
}
}
public class expression
{
public static void main(String a[])
{
System.out.println("This is the main class");
Fact obj1=new Fact();
obj1.input();
}
}

QUESTION N0 8:
class CryptarithmeticPuzzle
{
public void cal()
{
for (int T = 0; T <= 9; T++)
{
for (int O = 0; O <= 9; O++)
{
for (int G = 0; G <= 9; G++)
{
for (int D = 0; D <= 9; D++)
{
if (T != O && T != D && T != G && G != O && G
!= D && D != O)
{
int too = (100 * T) + (10 * O) + O;
int good = (1000 * G) + (100 * O) + (10 *
O) + D;
if (4 * too == good)
{
System.out.println("TOO + TOO
+ TOO + TOO = "
+ (4 * too));
System.out.println("GOOD = " +
good);
System.out.println("Therefore, T
="+T
+ ", O = " + O + ", G
="+G
+ ", and D = " + D);
System.out.println();
}
}
}

}
}
}
public class cryptarithmetic
{
public static void main(String a[])
{
System.out.println("This is the main class");
CryptarithmeticPuzzle obj1=new CryptarithmeticPuzzle();
obj1.cal();
}
}

You might also like