329 Geometry two circles Write a program that prompts the us
Solution
//Problem1.java
import java.util.Scanner;
public class Problem1
{
public static void main(String[] args)
{
Scanner userInput = new Scanner(System.in);
System.out.println(\"Enter an integer: \");
int number = userInput.nextInt();
System.out.println(\"Is \" + number + \" positive and divisibe by 2? \");
if(number > 0 && number%2 == 0)
System.out.println(\"true\");
else
System.out.println(\"false\");
System.out.println(\"Is \" + number + \" negative and divisibe by 2? \");
if(number < 0 && number%2 == 0)
System.out.println(\"true\");
else
System.out.println(\"false\");
System.out.println(\"Is \" + number + \" positive or divisibe by 2 but not both? \");
if(number > 0 && number%2 == 0)
System.out.println(\"false\");
else if(number > 0 || number %2 == 0)
System.out.println(\"true\");
else
System.out.println(\"false\");
System.out.println(\"Is \" + number + \" less than or equal to 30 or divisibe by 2? \");
if(number <= 30 && number%2 == 0)
System.out.println(\"true\");
else
System.out.println(\"false\");
}
}
/*
Output:
Enter an integer:
-90
Is -90 positive and divisibe by 2?
false
Is -90 negative and divisibe by 2?
true
Is -90 positive or divisibe by 2 but not both?
true
Is -90 less than or equal to 30 or divisibe by 2?
true
*/
//Problem2.java
import java.util.Scanner;
import java.util.Random;
public class Problem2
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
Random rand = new Random();
int correct = rand.nextInt(100);
while(true)
{
System.out.print(\"Guess a number 1-100: \");
int guess = input.nextInt();
if(guess < correct)
{
System.out.println(\"The guess is too low, try again.\");
}
else if(guess > correct)
{
System.out.println(\"The guess is too high, try again.\");
}
else if(guess == correct)
{
System.out.println(\"You are a good guesser!\");
break;
}
}
}
}
/*
Output:
Guess a number 1-100: 30
The guess is too high, try again.
Guess a number 1-100: 20
The guess is too high, try again.
Guess a number 1-100: 10
The guess is too low, try again.
Guess a number 1-100: 15
The guess is too low, try again.
Guess a number 1-100: 17
You are a good guesser!
*/
//Problem3.java
import java.util.Scanner;
import java.util.Random;
public class Problem3
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
double weight1, price1;
double weight2, price2;
System.out.print(\"Enter weight and price for packet 1: \");
weight1 = input.nextDouble();
price1 = input.nextDouble();
System.out.print(\"Enter weight and price for packet 2: \");
weight2 = input.nextDouble();
price2 = input.nextDouble();
double cost1 = price1*weight1;
double cost2 = price2*weight2;
if(cost1 < cost2)
System.out.println(\"Package 2 has worst price.\");
else
System.out.println(\"Package 1 has worst price.\");
}
}
/*
Output:
Enter weight and price for packet 1: 50 24.59
Enter weight and price for packet 2: 25 11.99
Package 1 has worst price.
*/
//Problem4.java
import java.util.Scanner;
import java.util.Random;
public class Problem4
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
double x1, y1, r1;
double x2, y2, r2;
double distance;
System.out.print(\"Enter Circle1\'s centre x-, y-coordinates, and radius: \");
x1 = input.nextDouble();
y1 = input.nextDouble();
r1 = input.nextDouble();
System.out.print(\"Enter Circle2\'s centre x-, y-coordinates, and radius: \");
x2 = input.nextDouble();
y2 = input.nextDouble();
r2 = input.nextDouble();
distance = Math.pow((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2), 0.5);
if (r2 >= r1 && distance <= (r2 - r1))
{
System.out.println(\"Circle 1 is inside Circle 2.\");
}
else if (r1 >= r2 && distance <= (r1 - r2) )
{
System.out.println(\"Circle 2 is inside Circle 1.\");
}
else if (distance > (r1 + r2))
{
System.out.println(\"Circle 1 does not overlap Circle 2.\");
}
else
{
System.out.println(\"Circle 1 overlaps Circle 2.\");
}
}
}
/*
Output:
Enter Circle1\'s centre x-, y-coordinates, and radius: 0.5 5.1 13
Enter Circle2\'s centre x-, y-coordinates, and radius: 1 1.7 4.5
Circle 2 is inside Circle 1.
*/



