Ok, so I know this is a noob question but I am having trouble getting this code to work. What the code is supposed to do is give you the diamter when you input the radius. I know my code is probably butchered but what am I doing wrong and why because I am trying to learn.
import java.util.Scanner;
public class Circle{
Scanner dd = new Scanner(ystem.in);
System.out.println("Whats is the radius?");
double r = dd.nextDouble();
public Circle(double r){
radius = r;
}
public double diameter(){
double d = radius * 2;
return d;
}
}
public class Tester{
public static void main(String args[]){
Circle cir1 = new Circle(35.5);
System.out.println(Circle.diameter)
}
}
You must put you code in a method. This block will cause an error :
Scanner dd = new Scanner(System.in);
System.out.println("Whats is the radius?");
double r = dd.nextDouble();
Next, in your main, you do Circle cirl = new Circle(35.5) and on the next line, you call Circle.diameter. You should call the diameter from you new instance as so cirl.diameter().
You could try something like this instead
import java.util.Scanner;
public class Circle{
private double radius;
public Circle(double r){
radius = r;
}
public double diameter(){
double d = radius * 2;
return d;
}
public static void main(String args[]){
Scanner dd = new Scanner(System.in);
System.out.println("Whats is the radius?");
double r = dd.nextDouble();
Circle cir1 = new Circle(r);
System.out.println(cir1.diameter())
}
}
You cannot have more than one public class in the same Java source file. So either create two source files (one for each class), or define one class inside the other, e.g.:
public class Test {
static class Circle {
...
}
...
public static void main(String[] args) {
...
}
}
Related
public class ConversionTester
{
public static void main(String[] args)
{
double g = gallonsToPints(4.0);
double f = feetToInches(2.0);
System.out.println();
System.out.println();
}
}
public class Conversion
{
public double gallon;
public double feet;
public static double gallonsToPints(double ga)
{
gallon = ga;
return gallon * 8;
}
public static double feetToInches(double fe)
{
feet = fe;
return feet * 12;
}
}
ConversionTester.java: Line 7: You may have forgotten to declare gallonsToPints(double) or it's out of scope.
ConversionTester.java: Line 8: You may have forgotten to declare feetToInches(double) or it's out of scope.
You have two options, either will work. The first is explicitly naming the class like
double g = Conversion.gallonsToPints(4.0);
double f = Conversion.feetToInches(2.0);
The second would be to add static import(s). Before public class ConversionTester like,
static import Conversion.gallonsToPints;
static import Conversion.feetToInches;
Cant set radius to count square: there're two methods, but how to force Java to count my square through radius that is random from randomNumber() ?
public class Figure {
public double randomNumber() {
return Math.random() * 11;
}
public double circleArea () {
return Math.PI*Math.pow(r,2); //how to assign a variable r in order to connect to randomNumber()??
}
public static void main(String[] args) {
Figure figure = new Figure();
System.out.println("r="+figure.randomNumber()+", s="+figure.circleArea());
}
}
Few different ways. You need to choose what works best for your scenario:
You can call the method from where required.
public double circleArea () {
return Math.PI*Math.pow(randomNumber(),2);
}
You can change the method to include a variable.
public double circleArea (double r) {
return Math.PI*Math.pow(r,2);
}
public static void main(String[] args) {
Figure figure = new Figure();
double r = figure.randomNumber();
System.out.println("r=" + r + ", s=" + figure.circleArea(r));
}
All you have to do is defining a private variable that holds the r value
public class Figure {
private double r;
public double randomNumber() {
r = Math.random() * 11;
return r;
}
public double circleArea () {
return Math.PI*Math.pow(r,2); //how to assign a variable r in order to connect to randomNumber()??
}
public static void main(String[] args) {
Figure figure = new Figure();
System.out.println("r="+figure.randomNumber()+", s="+figure.circleArea());
}
This question already has answers here:
What is float in Java?
(4 answers)
Closed 7 years ago.
I cant figure out the errors! But while compiling it shows error. Please help me out.....
// This program is used to find the area of a circle and a rectangle
// through constructor overloading concept.
class area {
float radius;
int l , b;
public area(float r) {
radius=r;
}
public area(int a , int d) {
l=a;
b=d;
}
public void display() {
System.out.println("Area of Circle is = "+(3.14*radius*radius));
System.out.println("Area of Rectangle is = "+(l*b));
}
}
class constadd {
public static void main(String arr[]) {
area c = new area(4.5);
c.display();
area e=new area(4,5);
e.display();
}
}`
Use double instead of float.
import java.util.*;
import java.lang.*;
import java.io.*;
class area {
double radius;
int l , b;
public area(double r) {
radius=r;
}
public area(int a , int d) {
l=a;
b=d;
}
public void display() {
System.out.println("Area of Circle is = "+(3.14*radius*radius));
System.out.println("Area of Rectangle is = "+(l*b));
}
}
class Ideone {
public static void main(String arr[]) {
area c = new area(4.5);
c.display();
area e=new area(4,5);
e.display();
}
}
As Anik mentioned, either change the constructor to take double as a parameter instead of float or while calling this constructor use suffix 4.5 with 'f' to specify that you want to pass float i.e., new area(4.5f);
I have such code which works well, but I wonder why method area is not possible to put in the main method
public class Circle {
Operation op;
double pi = 3.14;
double area(int radius) {
op = new Operation();
int rsquare = op.square(radius);
return rsquare * pi;
}
public static void main(String arg[]) {
Circle c = new Circle();
double s = c.area(5);
System.out.println(s);
}
class Operation {
int square(int n) {
return n * n;
}
}
}
Example which doesn't work:
public static void main(String arg[]) {
double area ( int radius){
op = new Operation();
int rsquare = op.square(radius);
return rsquare * pi;
}
The only way to nest method implementation code inside Java methods is with anonymous classes. In your case this would look like this (code has to be nested inside a class of sorts):
public static interface Circle {
double area(int radius);
}
public static interface Operation {
int square(int n);
}
public static void main(String arg[]) {
Circle c = new Circle() {
Operation op;
double pi = 3.14;
public double area(int radius) {
op = new Operation() {
public int square(int n) {
return n * n;
}
};
int rsquare = op.square(radius);
return rsquare * pi;
}
};
double s = c.area(5);
System.out.println(s);
}
Take
double area(){...}
Outside of the main method
Java doesn't support nested methods, even if it did, there would be no point to put a nonstatic method in a static method
The closest thing would be nested classes that contain methods, declared in the method
Could someone explain to me how reference (non-primitive) data types works? Mainly how to input data into them and how to check what data they are holding?
Could you use this code as an example please.
public class Example{
public static void main(String [] args){
Circle c= new Circle();
System.out.println():
}
}
public class Circle{
Circle round;
public Circle(){
}
public Circle numPlacment(){
round=new Circle(2); //I would like circle to contain the value of '2'
return round;
}
public String toString(){
StringBuilder b= new StringBuilder();
b.append(round);
return String.format("%4s",b);
}
}
Your code is somewhat nonsensical. It may be easier just to look at how it's supposed to be done:
public class Example{
public static void main(String [] args) {
// create a new circle with radius 2
Circle c= new Circle(2);
// Print that circle
System.out.println(c);
}
}
class Circle {
// The instance variable that stores the radius for this circle
double radius;
// Create a new Circle given a radius
public Circle(double radius) {
// assign the given radius parameter to the instance variable
this.radius = radius;
}
public String toString() {
StringBuilder b= new StringBuilder();
b.append(radius);
return String.format("%4s",b);
}
}