Question

In: Computer Science

1) Write a functional program in Java that can calculate the volume and surface area of...

1) Write a functional program in Java that can calculate the volume and surface area of a sphere and a cube

2) Write a procedural program in Java that can calculate the volume and surface area of a sphere and a cube

3) Write an Object Oriented Program in Java that can find the volume and surface area of a sphere and cube

Solutions

Expert Solution

Procedural Program:

import java.util.*;

class Main {

public static void main(String[] args) {

double radius, side,SAsphere,VSphere,SAcube,Vcube;

Scanner sc=new Scanner(System.in);

System.out.println("Enter radius of sphere: ");

radius=sc.nextDouble();

System.out.println("Enter Side of cube: ");

side=sc.nextDouble();

SAsphere=3.14*4*radius*radius;

VSphere=(4/3)*3.14*radius*radius*radius;

SAcube=6*side*side;

Vcube=side*side*side;

System.out.println("Surface area of sphere is : "+SAsphere);

System.out.println("Volume of Sphere: "+VSphere);

System.out.println("Surface area of Cube is "+SAcube);

System.out.println("Volume of Cube: "+Vcube);

}

}

2)Functional Program:

import java.util.*;

class Main {

public static double volumeSphere(double radius){

return (4/3)*3.14*radius*radius*radius;

}

public static double SAsphere(double radius){

return 3.14*4*radius*radius;

}

public static double volumeCube(double side){

return side*side*side;

}

public static double SACube(double side){

return 6*side*side;

}

public static void main(String[] args) {

double radius, side,SAsphere,VSphere,SAcube,Vcube;

Scanner sc=new Scanner(System.in);

System.out.println("Enter radius of sphere: ");

radius=sc.nextDouble();

System.out.println("Enter Side of cube: ");

side=sc.nextDouble();

System.out.println("Surface area of sphere is : "+SAsphere(radius));

System.out.println("Volume of Sphere: "+volumeSphere(radius));

System.out.println("Surface area of Cube is "+SACube(side));

System.out.println("Volume of Cube: "+volumeCube(side));

}

}

3) Object Oriented:

import java.util.*;

class Calculation {

public static double volumeSphere(double radius){

return (4/3)*3.14*radius*radius*radius;

}

public static double SAsphere(double radius){

return 3.14*4*radius*radius;

}

public static double volumeCube(double side){

return side*side*side;

}

public static double SACube(double side){

return 6*side*side;

}

}

class Main{

public static void main(String[] args) {

Calculation cal=new Calculation();

double radius, side,SAsphere,VSphere,SAcube,Vcube;

Scanner sc=new Scanner(System.in);

System.out.println("Enter radius of sphere: ");

radius=sc.nextDouble();

System.out.println("Enter Side of cube: ");

side=sc.nextDouble();

System.out.println("Surface area of sphere is : "+cal.SAsphere(radius));

System.out.println("Volume of Sphere: "+cal.volumeSphere(radius));

System.out.println("Surface area of Cube is "+cal.SACube(side));

System.out.println("Volume of Cube: "+cal.volumeCube(side));

}

}

if you like the answer please provide a thumbs up.


Related Solutions

Write a C++ program to calculate the sphere volume and surface area. Provided that the sphere...
Write a C++ program to calculate the sphere volume and surface area. Provided that the sphere volume = and the sphere surface area = , where r is the radius. 1. Read r from the user using cin statement. 2. Calculate the volume (Vol) and the surface area (Area) of a sphere. 3. Print r, Vol, and Area in a suitable messages.
Part One: Write an interactive program to calculate the volume and surface area of a three-dimensional...
Part One: Write an interactive program to calculate the volume and surface area of a three-dimensional object. Use the following guidelines to write your program: Create a word problem that involves calculating the volume and surface area of a three-dimensional object. Choose one of the following: Cube: surface area 6 s2 , volume s3 Sphere: surface area 4πr2 , volume (4.0/3.0) π r3 Cylinder: surface area 2π r2 + 2 π r h, volume π r2 h Cone: surface area...
Write a program in C++ that solves this problem Calculate the area and volume of a...
Write a program in C++ that solves this problem Calculate the area and volume of a sphere problem. Inside a for loop, vary the radius from 10 to 40  with a step or increment of 5 and calculate the area and volume Your radius will be equal to your loop counter. All calculations should have 2 decimal places, but the radius should have zero decimal places and any number of 1,000 or more should have a comma. Print the radius, area,...
Write a C++ program for the following problem: Calculate and print the area and volume of...
Write a C++ program for the following problem: Calculate and print the area and volume of a cone inside a While  loop that goes from 1 to 20 with a step of .5. (the step is 1/2 or Point 5, so you go 10, 10.5,11, 11.5) Note: Your loop variable will need to be a double data type Use two decimal places on all numbers that are double data type. This will be a table with 3 columns. Don't worry about...
User the Scanner class for your input Write a java program to calculate the area of...
User the Scanner class for your input Write a java program to calculate the area of a rectangle. Rectangle Area is calculated by multiplying the length by the width   display the output as follow: Length =   Width = Area = Load: 1. Design (Pseudocode ) 2. Source file (Java file, make sure to include comments) 3. Output file (word or pdf or jpig file)
Write most basic C++ program possible that calculates volume and surface area of five shapes (Cube,...
Write most basic C++ program possible that calculates volume and surface area of five shapes (Cube, Sphere, Prism, Cylinder, Cone), where each shape has its own class (with a volume function, surface area function, constructor, as well as get and set functions), and the shape dimensions are private class members. Include input and display functions.
know the mathematical relationship between surface area and volume, and explain how surface to area volume...
know the mathematical relationship between surface area and volume, and explain how surface to area volume affects heat loss. Which can be smaller endotherms or ectotherms  
write a java program for area and perimeter of right triangle
write a java program for area and perimeter of right triangle
Writing a Java Code Requirements of the JAVA program: Your task is to calculate geometric area...
Writing a Java Code Requirements of the JAVA program: Your task is to calculate geometric area for 3 shapes(square, rectangle and circle). You need to build a menu that allows users to enter options. Possible options are 'S' for square, 'R' for rectangle and 'C' for circle. HINT: you can use switch statement to switch on string input Invalid input should throw a message for the user. Example: Invalid input, please try again Each options should ask users for relevant...
Write a program that reads the radius of a sphere and calculates it's volume and surface...
Write a program that reads the radius of a sphere and calculates it's volume and surface area. Format results to 4 decimal places. The radius=3.2 Your program should output volume and surface area.  Repeat same steps for program2, but this time use the following input values: a= ꟷ2.4 , b = 4.5 . Your program should output sum, Fun1, and Fun2. It's for C++
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT