Java Programs* 1. Write a function that uses a definite 10 loop that initializes
ID: 3738722 • Letter: J
Question
Java Programs*
1. Write a function that uses a definite 10 loop that initializes a loop control variable named increased to 10 and continues while (increased>100),
Increment the loop by 2 , and include the println output (“keep going”) with the loop.
2. Write a function that reads a students age and height(feet,inches), and use the logical or to display either “selected” or “not selected”, if the candidates age is under 18 or height is under 25 inches respectively.
3. a. Write a program to sort an array of 100 positive integers in ascending order;
b. create a class “circle” which has 1 int radius and 1 double Pi, and a member function , “GetArea” that computes and displays the area (A=(pi*radius)^2);
c. create a class “circumference” which will inherit radius and pi from Circle (extends), create a function getCir(), to compute and display circumference (C=2*(pi*radius).
For each practice questions; the program must object-oriented.
Explanation / Answer
NOTE: I have completed your assignment and below are the details. Please check and let me know if you have any questions. I will revert back within 24 hours.
1)
Code:
Unix Terminal> cat ex1.java
public class ex1{
public static void main(String[] args){
// calling function runLoop with initial counter value 10, loop until 100 and step value 2
runLoop(10, 100, 2);
}
static void runLoop(int increased, int loopUntil, int stepValue){
for(; increased <= loopUntil; increased += stepValue)
System.out.println("keep going");
}
}
Code Execution output:
Unix Terminal> java ex1
keep going
keep going
keep going
keep going
keep going
keep going
keep going
keep going
keep going
keep going
keep going
keep going
keep going
keep going
keep going
keep going
keep going
keep going
keep going
keep going
keep going
keep going
keep going
keep going
keep going
keep going
keep going
keep going
keep going
keep going
keep going
keep going
keep going
keep going
keep going
keep going
keep going
keep going
keep going
keep going
keep going
keep going
keep going
keep going
keep going
keep going
Unix Terminal>
2)
Code:
Unix Terminal> cat ex2.java
import java.util.Scanner;
public class ex2{
public static void main(String[] args){
readAgeandHeight();
}
static void readAgeandHeight(){
// declaring scanner object to read user input
Scanner sc = new Scanner(System.in);
// reading student age
System.out.println("Enter student age: ");
int age = sc.nextInt();
// reading student height
System.out.println("Enter student height: ");
double height = sc.nextFloat();
// verifying if age under 18 and height under 25
if (age < 18 || height < 25)
System.out.println("not selected");
else
System.out.println("selected");
}
}
Unix Terminal>
Code output:
Unix Terminal> java ex2
Enter student age:
19
Enter student height:
25
selected
Unix Terminal> java ex2
Enter student age:
20
Enter student height:
33
selected
Unix Terminal> java ex2
Enter student age:
17
Enter student height:
20.2
not selected
Unix Terminal>
3a)
Since it is difficult to read 100 positive integers. I have made the number of numbers user can provide as dynamic. So that user can provide any number and based on it the number of integers to be read is decided.
Below is an output example where we have read 5 integers and we can run the program to read for 100 or any number of positive integers.
Code:
Unix Terminal> cat ex3a.java
import java.util.Arrays;
import java.util.Scanner;
public class ex3a{
public static void main(String[] args){
// declaring scanner object for reading user input
Scanner sc = new Scanner(System.in);
// getting number of integers user want to read
System.out.println("Enter how many numbers? ");
int n = sc.nextInt();
// declaring array variable based on number of numbers user want to provide
int arr[] = new int[n];
// reading integers upto number of numbers user want to read
for(int i = 0; i < n; i++){
System.out.println("Enter " + i + " number: ");
int x = sc.nextInt();
arr[i] = x;
}
// sorting the array
Arrays.sort(arr);
// printing sorted array elements
System.out.println("Sorted array is:");
for(int i = 0; i < n; i++){
System.out.println(arr[i]);
}
}
}
Unix Terminal>
Code output:
Unix Terminal> java ex3a
Enter how many numbers?
5
Enter 0 number:
10
Enter 1 number:
98
Enter 2 number:
294
Enter 3 number:
1
Enter 4 number:
28
Sorted array is:
1
10
28
98
294
Unix Terminal>
3b)
Code:
Unix Terminal> cat ex3b.java
import java.util.Scanner;
import java.lang.*;
public class ex3b{
public static void main(String[] args){
// creating a circle object of Circle class
Circle cr = new Circle();
// calling getArea method of Circle class
cr.getArea();
}
}
class Circle{
// initializing class variables
int radius;
double pi = 3.14;
// defining member function getArea()
void getArea(){
Scanner sc = new Scanner(System.in);
// reading radius of circle
System.out.println("Enter radius of the circle as integer? ");
radius = sc.nextInt();
// calculating area of circle and displaying it
double area = Math.pow((pi * radius), 2);
System.out.println("Area of cicle is: " + area);
}
}
Unix Terminal>
Code output:
Unix Terminal> javac ex3b.java
Unix Terminal> java ex3b
Enter radius of the circle as integer?
8
Area of cicle is: 631.0144
Unix Terminal> java ex3b
Enter radius of the circle as integer?
4
Area of cicle is: 157.7536
Unix Terminal> java ex3b
Enter radius of the circle as integer?
2
Area of cicle is: 39.4384
Unix Terminal>
3c)
Code:
Unix Terminal> cat ex3c.java
public class ex3c{
public static void main(String[] args){
// Creating an object for Circumference class
Circumference cr = new Circumference();
// calling a method of circumference class
cr.getCir();
}
}
// defining a Circumference circle and extending Circle class
class Circumference extends Circle{
// defining getCir method
void getCir(){
// calling getArea method of Circle class to obtain radius from the user
getArea();
// computing circumference of the circle and displaying it
double c = 2 * (pi * radius);
System.out.println("Circumference of the cirlce is: " + c);
}
}
Unix Terminal>
Code output:
Unix Terminal> java ex3c
Enter radius of the circle as integer?
4
Area of cicle is: 157.7536
Circumference of the cirlce is: 25.12
Unix Terminal> java ex3c
Enter radius of the circle as integer?
6
Area of cicle is: 354.9456
Circumference of the cirlce is: 37.68
Unix Terminal>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.