public class Part1
{
public static void main(String args [])
{
DecimalFormat df=new DecimalFormat("###.##");
double v;
Scanner sc = new Scanner( System.in );
double radius;
System.out.println( "Enter radius of the basketball: " );
radius = sc.nextDouble();
System.out.println("The volume of the basketball is " +df.format(v));
}
public static int volume (int v)
{
v= ( 4.0 / 3.0 ) * Math.PI * Math.pow( radius, 3 );
}
}
Basically, i have to let the user input the radius of the basketball and you have to calculate the volume of the basketball. the codes works perfectly, but i don't know how to do it in function method?
I'm fairly certain you need to return double and pass the radius as a double to your volume method. You also need to call the function and get the value. You should try and restrict the lexical scope of your variables. Something like,
public static void main(String args[]) {
DecimalFormat df = new DecimalFormat("###.##");
Scanner sc = new Scanner(System.in);
System.out.println("Enter radius of the basketball: ");
double radius = sc.nextDouble();
double v = volume(radius);
System.out.println("The volume of the basketball is " + df.format(v));
}
public static double volume(double radius) {
return (4.0 / 3.0) * Math.PI * Math.pow(radius, 3);
}
Related
I'm having difficulty trying to print the result of the static method calcArea, which takes the int radius as parameter to calculate the area of a circle and returns the area.
Here's the code below 👇 Any help would be appreciated.
public class CircleArea {
public int radius;
public static void main(String[] args) {
Scanner input = new Scanner (System.in);
System.out.print("Enter the radius of the circle: ");
int radius = input.nextInt();
System.out.print("The area of the circle is:" + calcArea()); <-- ERROR HERE
}
public static double calcArea(int radius){
double area = Math.PI * Math.pow(radius, 2);
return area;
}
}
Your call to calcArea needs a parameter passed in. Probably calcArea(radius).
The function calcArea() takes the value of radius and then returns area. To do this, you need to pass an argument to calcArea(). So, your code should be like this:
System.out.print("The area of the circle is:" + calcArea(radius));
The error you're getting clearly points out that you're missing an argument.
call method calcArea, you need give a parameter,Here are the correct example"
public static void main(String[] args) {
Scanner input = new Scanner (System.in);
System.out.print("Enter the radius of the circle: ");
int radius = input.nextInt();
System.out.print("The area of the circle is:" + calcArea(radius));
}
I am new to Java and trying to make a basic body mass calculator.
My problem is I need to ask the questions, convert the measurements and then pass it to another method then display the results in a separate method.
I've added my code below but keep getting a 1.0 returned as the answer each time.
import java.util.Scanner;
public class calcBMI {
public static void main(String[] args)
{
Scanner keyboard = new Scanner( System.in );
System.out.print("Enter weight in pounds: ");
double weightInPounds = keyboard.nextDouble();
double weightInKg = (weightInPounds / 2.2);
System.out.print("Enter height in inches: ");
double heightInInches = keyboard.nextDouble();
double heightInMeters = (heightInInches / 0.254);
double resultBMI = 1;
displayResults(resultBMI);
}
public static double bodyMassIndex(double weightInKg, double
heightInMeters)
{
double resultBMI = weightInKg / Math.pow(heightInMeters, 2) * 1.375;
return resultBMI;
}
public static void displayResults(double resultBMI)
{
System.out.printf("The calculated body mass index was: " + resultBMI);
System.out.println();
}
}
Updated code, now getting;
Enter weight in pounds: 180
Enter height in inches: 68
The calculated body mass index was: 1.1415618118905313E-5
BUILD SUCCESSFUL (total time: 3 seconds)
import java.util.Scanner;
public class calcBMI {
public static void main(String[] args)
{
Scanner keyboard = new Scanner( System.in );
System.out.print("Enter weight in pounds: ");
double weightInPounds = keyboard.nextDouble();
double weightInKg = (weightInPounds / 2.2);
System.out.print("Enter height in inches: ");
double heightInInches = keyboard.nextDouble();
double heightInMeters = (heightInInches / 0.0254);
displayResults(bodyMassIndex(weightInKg, heightInMeters));
}
public static double bodyMassIndex(double weightInKg, double heightInMeters)
{
return (weightInKg / Math.pow(heightInMeters, 2));
}
public static void displayResults(double resultBMI)
{
System.out.printf("The calculated body mass index was: " + resultBMI);
System.out.println();
}
}
You are not calling the bodyMassIndex method in your code at all. Change
displayResults(resultBMI);
to
displayResults(bodyMassIndex(weightInKg, heightInMeters));
resultBMI equals 1, so of course the output would always be :
"The calculated body mass index was: 1"
Full code:
public static void main(String[] args) {
System.out.print("Enter weight in pounds: ");
double weightInPounds = keyboard.nextDouble();
double weightInKg = (weightInPounds / 2.2);
System.out.print("Enter height in inches: ");
double heightInInches = keyboard.nextDouble();
double heightInMeters = (heightInInches / 0.254);
// You can get rid of the resultBMI variable
displayResults(bodyMassIndex(weightInKg, heightInMeters));
}
You get 1.0 because you hard coded it as such.
Change this:
double resultBMI = 1;
To:
double resultBMI = bodyMassIndex(weightInKG, heightInMeters);
By the way, you could also have returned BMI directly in the method and there is no need to multiply by 1.375 anymore since you are already supplying weight in KG:
public static double bodyMassIndex(double weightInKg, double heightInMeters)
{
return (weightInKg / (heightInMeters*heightInMeters));
}
Add on:
Your conversion from inches to meters is wrong as well. It should be:
double heightInMeters = (heightInInches * 0.0254);
So I've spent the last couple days working on this program, and I've hit a roadblock. I am trying to make a calculator that uses specific programs with user input to calculate the Surface area or Volume of multiple shapes. You might recognize this as project 8.5 from the Horstmann java concepts book.
I don't know how to call the methods from one file to another, and need to figure it out.
Here's the code:
import java.util.Scanner;
public class p85SantoCharlie{
public static double sphereVolume(double r){
Scanner in = new Scanner(System.in);
System.out.println("Please enter your Radius");
r = in.nextDouble();
double volume = (4/3) * Math.PI * r * r * r;
System.out.println(volume);
return volume;
}
public static double sphereSurface(double r){
Scanner in = new Scanner(System.in);
System.out.println("Please enter your Radius");
r = in.nextDouble();
double surface = 4 * Math.PI * r * r ;
System.out.println(surface);
return surface;
}
public static double cyliderVolume(double r, double h){
Scanner in = new Scanner(System.in);
Scanner out = new Scanner(System.in);
System.out.println("Please enter your Radius");
r = in.nextDouble();
System.out.println("Please enter your Height");
h = out.nextDouble();
double volume = Math.PI * r * r * h ;
System.out.println(volume);
return volume;
}
public static double cylinderSurface(double r, double h){
Scanner in = new Scanner(System.in);
Scanner out = new Scanner(System.in);
System.out.println("Please enter your Radius");
r = in.nextDouble();
System.out.println("Please enter your Height");
h = out.nextDouble();
double surface = 2 * Math.PI * r * r * h ;
System.out.println(surface);
return surface;
}
public static double coneVolume(double r, double h){
Scanner in = new Scanner(System.in);
Scanner out = new Scanner(System.in);
System.out.println("Please enter your Radius");
r = in.nextDouble();
System.out.println("Please enter your Height");
h = out.nextDouble();
double volume = Math.PI * r * r * h / 3 ;
System.out.println(volume);
return volume;
}
public static double coneSurface(double r, double h){
Scanner in = new Scanner(System.in);
Scanner out = new Scanner(System.in);
System.out.println("Please enter your Radius");
r = in.nextDouble();
System.out.println("Please enter your Height");
h = out.nextDouble();
double surface = Math.PI * r * (r + Math.pow(( r * r + h * h), .5));
System.out.println(surface);
return surface;
}
}
And here is the main file:
import java.util.Scanner;
public class p85SantoCharlieMain{
public static void main(String[] args){
p85SantoCharlie mainProgram = new p85SantoCharlie();
Scanner in = new Scanner(System.in);
System.out.println("Please select a shape");
System.out.println("your choices are: Sphere, Cylinder, and Cone");
String answer1 = in.next();
String answer1Caps = answer1.toUpperCase();
System.out.println("Fantastic! now select a formula");
System.out.println("your choices are: surface area or volume");
String answer2 = in.next();
String answer2Caps = answer2.toUpperCase();
if (answer1Caps.equals("SPHERE")&& answer2Caps.equals("SURFACE AREA")){
mainProgram.sphereSurface();
}
}
}
Thanks!
If the methods are static you can call them using the class name
p85SantoCharlie.sphereVolume(1.1);
And if they aren't static initialize new class instance and use it to call
p85SantoCharlie p = new p85SantoCharlie();
p.sphereVolume(1.1);
Three problems:
First, all of your calculation methods in p85SantoCharlie are static. You don't need an instance of p85SantoCharlie to invoke them. You should get rid of the line where you do new p85SantoCharlie() and invoke your methods like
p85SantoCharlie.sphereVolume(r);
Second, you have declared your calculation methods as taking parameters, but you don't know those parameters when you invoke them. For example, sphereVolume() is declared as taking double r. But you don't know the value of r until you read it in within the sphereVolume() method. So that can't work. You need to change your main method to ask for the radius, then pass it to sphereVolume as shown above.
Third, all of those Scanners! Get rid of them all except for the one in main. You will be passing in the value of r (or whatever) when you invoke the calculation method. Just use what is passed in.
Ugly, unreadable code.
Java's an object-oriented language. I would expect that you'd start with a Shape interface:
public interface Shape {
double surfaceArea();
double volume();
}
I'd expect to see subclasses for different types that would do the calculation appropriately. Here's a Sphere implementation:
public class Sphere implements Shape {
private final double radius;
public Sphere(double radius) {
if (radius <= 0.0) throw new IllegalArgumentException("radius must be positive");
this.radius = radius;
}
public double surfaceArea() {
return 4.0*Math.PI*this.radius*this.radius;
}
public double volume() {
return 4.0*Math.PI*this.radius*this.radius*this.radius/3.0;
}
}
Here's a Cylinder implementation:
public class Cylinder implements Shape {
private final double radius;
private final double height;
public Cylinder(double radius, double height) {
if (radius <= 0.0) throw new IllegalArgumentException("radius must be positive");
if (height <= 0.0) throw new IllegalArgumentException("height must be positive");
this.radius = radius;
this.height = height;
}
public double surfaceArea() {
return 2.0*Math.PI*this.radius*this.radius + this.radius*this.height;
}
public double volume() {
return Math.PI*this.radius*this.radius*this.height;
}
}
I'd expect to see your driver class interact with Shape objects to solve the problem.
You'll want to consider a virtual constructor/factory for Shape, because you won't want to clutter your code with if/else constructs.
My opinion: Object-oriented programming was invented to solve two problems:
Encapsulation of state and behavior into a single abstract data type.
Inheritance and polymorphism to eliminate if/else and switch statements.
Do not clutter your code with "if sphere and area" kinds of decisions. Let polymorphism handle it for you.
New programmers tend to spend too much time worrying about user interfaces. Concentrate on the functionality and get that working first. Don't create a bunch of text question/answer code and leave the meat of the problem undone.
I've messed around with this for an hour now and can't get it to work. I've also looked this question up but the wording used in answers I've found haven't helped me at all.
Any help would be much appreciated.
Also, the invocation in question is at the very end of the program, inside main.
import java.util.Scanner;
public class Area {
Scanner input = new Scanner (System.in);
/**
* #param args the command line arguments
*/
public static void areaTriangle (double height, double length){
System.out.println((height * length) * .5);
}
public static void areaRectangle (double height, double length,
double width){
System.out.println(height * length * width);
}
public static void areaCircle (double radius){
System.out.println(Math.PI * (radius * radius));
}
public void calcArea (){
double i;
System.out.println("Which shape's area would you like to calculate?: \n"
+ "Enter '1' for Triangle \n"
+ "Enter '2' for Rectangle \n"
+ "Enter '3' for Circle \n"
+ "Enter '0' to quit the program");
i = input.nextDouble();
if (i == 1)
{
System.out.print("Enter the height of your triangle: ");
double height = input.nextDouble();
System.out.print("Enter the height of your length: ");
double length = input.nextDouble();
areaTriangle(height, length);
}
else if ( i == 2)
{
System.out.print("Enter the height of your rectangle: ");
double height = input.nextDouble();
System.out.print("Enter the length of your rectangle: ");
double length = input.nextDouble();
System.out.print("Enter the width of your rectangle: ");
double width = input.nextDouble();
areaRectangle(height, length, width);
}
else if ( i == 3)
{
System.out.print("Enter the radius of your circle");
double radius = input.nextDouble();
areaCircle(radius);
}
else if ( i == 0)
return;
else
System.out.println("Please input a number from 0 - 3");
}
public static void main(String[] args) {
calcArea();
}
}
put static in calcArea
public static void calcArea ()
or you can do this in your main
public static void main(String[] args) {
YourClassName variable= new YourClassName();
variable.calcArea();
}
just change the "YourClassName" to the name of your class and also you can change the variable object
I have spent a while trying to figure out why it won't use the decimal format in my java code. I have declared the decimal format with the other integers but is still coming up with errors. Here is my code:
public class Assign31
{
public static void main(String [] args)
{
DecimalFormat speedPattern = new DecimalFormat( "00.00" );
int gearRadius = 100;
double pi = Math.PI;
int cadence = 90;
double gearspeed = gearRadius * pi;
double speedmph = gearspeed % cadence;
System.out.println("The speed of the cyclist on a bike with gear 100.0 and cadence 90 is " +
speedmph + " mph.");
System.out.println("The speed (rounded) of the cycleist on the bike with gear 100.0 and cadence 90 is " +
speedPattern.format(speedmph));
}
}
You need to import it:
import java.text.DecimalFormat;
public class Assign31 {
public static void main(String [] args) {
DecimalFormat speedPattern = new DecimalFormat( "00.00" );
// etc.
import java.text.DecimalFormat;
public class Assign31 {
public static void main(String[] args) {
// Problem 1
// declare variables
DecimalFormat speedPattern = new DecimalFormat("00.00");
int gearRadius = 100;
double pi = Math.PI;
int cadence = 90;
// initialze data for first scenario
double gearspeed = gearRadius * pi;
// calculate speed
double speedmph = gearspeed % cadence;
// display speed
System.out
.println("The speed of the cyclist on a bike with gear 100.0 and cadence 90 is "
+ speedmph + " mph.");
System.out
.println("The speed (rounded) of the cycleist on the bike with gear 100.0 and cadence 90 is "
+ speedPattern.format(speedmph));
// end problem 1
}
}