How do I access a Class in Java in my main class - java

How do I access my "Cone" class from my "Main" class. The error I am getting is the dot operator in my Main class. I'm really new to java so Im really confused as to how you access what I have created in the "Cone" class any details would be greatly appreciated. Thank you
class Cone {
public double r;
public double h;
public void setRadius() {
r = r;
}
public void setHeight() {
h = h;
}
public double volume(double r, double h) {
double v;
v = Math.PI * Math.pow(r, 2) * (h / 3);
return v;
}
public double surfaceArea(double r, double h) {
double sa;
sa = Math.PI * r * (r + Math.sqrt(Math.pow(h, 2) + Math.pow(r, 2)));
return sa;
}
}
class Main {
public static void main(String args[]) {
double r;
double h;
Cone cone = new Cone();
for (double i = 0; i < 10; i++) {
cone.volume(r);
cone.volume(h);
System.out.printf("Volume = %d\n", cone.Volume());
}
}
}

A quick fix for you
class Cone
{
public double r;
public double h;
public void setRadius(double r)
{
this.r = r;
}
public void setHeight(double h)
{
this.h = h;
}
public double volume()
{
double v;
v = Math.PI * Math.pow(r,2) * (h/3);
return v;
}
public double surfaceArea()
{
double sa;
sa = Math.PI* r * (r + Math.sqrt(Math.pow(h,2) + Math.pow(r,2)));
return sa;
}
}
class Main
{
public static void main(String args[ ])
{
double r;
double h;
Cone cone = new Cone();
for (double i = 0; i < 10; i++)
{
// r and h are not set yet
r = h = i; // maybe?
cone.setRadius(r);
cone.setHeight(h);
System.out.printf("Volume = %d\n", cone.volume( ));
}
}
}

I think your mistake is that cone.Volume() should be cone.volume(). And also, you need to provide parameters. I would modify the Cone class by getting rid of those set methods and adding a parameterized constructor like so:
public Cone(double r, double h)
{
//initialize fields
this.r = r;
this.h = h
}

first error update you code
public void setRadius(double r)
{
this.r = r;
}
public void setHeight(double h)
{
this.h = h;
}

You can set the values by setter, but in your code the setter for the variables don't have any parameter to accept:
public void setRadius(double r) {
this.r = r;
}

First off, you would probably want a constructor that can initialize the dimensions
public Cone(double r, double h){
this.r = r;
this.h = h;
}
Next, your volume method expects two double arguments
//use this
cone.volume(r,h);
//not this
cone.volume(r);
cone.volume(h);
Finally, since this method returns a double with the volume, you will want to have another double variable to catch it for later use
double v = cone.volume(r,h);

Declare your Main class as public. (Suggestion)
cone.volume() requires 2 parameters. You can not append 2 parameters one after another. Instead of cone.volume(r); cone.volume(h); You need to pass r and h in single statement like : cone.volume(r,h); Because you have mentioned original volume(par1, par2). Assign some value to r and h either by setter or by using constructor.
For a basic start hardcode some values to r and h in Main class only.

Related

Java error unable to identify mistake

This is Java code, I have created 4 classes 3 constructor and I am getting error of:
method area in class Rect cannot be applied to given types
There is a similar error for rest of 2 class as well. In this program basically I have created 4 classes, 1 for calculating area of rect, 1 for calculating area of Tri and 1 for calculating area of Square and last one is to access main function.
I have created 3 constructor for all the 3 classes rect tri and square and I am unable to spot the mistake in this program.
class Rect //1st class rect
{
double l, b; //variables
Rect(double l, double b) //constructor for rect
{
this.l = l;
this.b = b;
}
double area(double l, double b) //method to cal Rect area
{
return l * b;
}
}
class Square //square class
{
double s;
Square(Double s) //constructor for class
{
this.s = s;
}
double area(double s) //method to cal area for square
{
return s * s;
}
}
class Tri // class for triangle
{
double l, b, h; //variables
Tri(double l, double b, double h) // constructor for tri
{
this.l = l;
this.h = h;
this.b = b;
}
double area(double l, double b, double h) //method to cal area for tri
{
return 0.5 * l * b * h;
}
}
class Area3 {
public static void main(String args[]) {
Rect r = new Rect(10, 10); //constructor initialization for Rect
Square s = new Square(15.0);//constructor initialization for Square
Tri t = new Tri(10.0, 20.0, 30.0);//constructor initialization for Tri
System.out.print(" " + r.area() + "" + s.area() + "" + t.area()); //print areas
}
}
Your area method declarations state that the area methods take in arguments. With those declarations you can't say
Rect r = new Rect(1,4);
r.area();
Simply remove the double argument values from the area methods
You have to create area method without parameters, here the solution,
class Rect // 1st class rect
{
double l, b; // variables
Rect(double l, double b) // constructor for rect
{
this.l = l;
this.b = b;
}
double area(){
return this.l * this.b;
}
double area(double l, double b) // method to cal Rect area
{
return l * b;
}
}
class Square // square class
{
double s;
Square(Double s) // constructor for class
{
this.s = s;
}
double area(){
return this.s * this.s;
}
double area(double s) // method to cal area for square
{
return s * s;
}
}
class Tri // class for triangle
{
double l, b, h; // variables
Tri(double l, double b, double h) // constructor for tri
{
this.l = l;
this.h = h;
this.b = b;
}
double area(){
return 0.5 * this.l * this.b * this.h;
}
double area(double l, double b, double h) // method to cal area for tri
{
return 0.5 * l * b * h;
}
}
class Area3 {
public static void main(String args[]) {
Rect r = new Rect(10, 10); // constructor initialization for Rect
Square s = new Square(15.0);// constructor initialization for Square
Tri t = new Tri(10.0, 20.0, 30.0);// constructor initialization for Tri
System.out.print(" " + r.area() + " and " + s.area() + " and " + t.area()); // print
// areas
}
}
Hope this help, BTW it's work in my PC.
look at your contractors, they all receive an arguments.
and all your area()'s are getting also a arguments.
but!! in your main, you are calling the area() and do not give any values.
just delete from area()'s functions the receiving arguments.

Creating a method to increase height and width of an oblong by amounts entered by the user

Ok so for class I have been working on some oblong questions based off an oblong class. The question I am having an issue with is to create a method to increase the height and width of the oblong by a user defined amount.
This is my main:
import java.util.Scanner;
public class Oblong6
{
public static void main(String [] args)
{
Oblong ob1 = new Oblong();
Scanner keyboardIn = new Scanner(System.in);
Oblong ob = new Oblong();
double h, w, x;
System.out.print ("Enter the height of the oblong:");
h = keyboardIn.nextDouble();
System.out.print ("Enter the width of the oblong:");
w = keyboardIn.nextDouble();
System.out.print (" Enter the amount to increment by ");
x = keyboardIn.nextInt();
ob.setHeight(h);
ob.setWidth(w);
ob.setX(x);
System.out.println (ob.incHeight());
System.out.println (ob.incWidth());
System.out.println("Height " + h);
System.out.println("Width " + w);
}
}
And this is the method I have created in the oblong class to increase them:
public class Oblong
{
// instance variables
private double height;
private double width;
private double x;
// constructor
public Oblong()
{
height = 0.0;
width = 0.0;
x = 0.0;
}
// methods
public double getHeight()
{
return height;
}
public double getWidth()
{
return width;
}
public double setX(double x)
{
return x;
}
public void setWidth(double w)
{
width = w;
}
public void setHeight(double h)
{
height = h;
}
public double calculateArea()
{
return width * height;
}
public double calculatePerimeter()
{
return width + height * 2;
}
public boolean isSquare()
{
if(height == width)
{
return true;
}
else
{
return false;
}
}
public double incHeight()
{
{
return height + x ;
}
}
public double incWidth()
{
{
return width + x ;
}
}
}// end of class
but it only ever prints out the original height and width.
Your code:
public double incHeight()
{
{
return height + x ;
}
}
That just adds two numbers and returns the result.
It doesn't do anything else. The same is true for your other methods.
Whereas the purpose of the method seems to be to alter the state of the underlying object. But as said; the current implementation does not alter that state.
Hope that is good enough to help you to resolve your problem on your own.
Side note: read about Java syntax. Your extra pair of braces ... doesn't do anything either.
Whit this:
public double incWidth()
{
{
return width + x ;
}
}
You are returning width + 1 but you are not modifying the private attribute. Just do this:
public double incWidth(){
this.width = this.width + 1;
return this.width;
}
Also in setters you don't need to return anything. To change an attribute inside a class do something like:
private double value;
private void setValue( double value ) {
this.value = value;
}
With this.value you are refering to the private value inside the class. Without this you are refering to the parameter value of the method setValue.
Further reading:
How do getters and setters work?
Your instance variable x has not been set, hence the height + x will return height + 0.0.
change this:
public double setX(double x)
{
return x;
}
To this:
public double setX(double x)
{
this.x = x;
}
This will return value to be displayed, but it should be set for later use, so you need this :
public void incHeight()
{
setHeight(height + x) ;
}
and then :
System.out.println("Height " + height); // height not h

Calling a constructor from a class in the main method?

I've read some of the other questions, but still couldn't seem to figure out how to get mine to work, any help is appreciated. The code I have so far is given below. I want to be able to call a newPointParameters to create a new class.
public class Lab4ex1 {
public static void main(String[] args) {
System.out.println("" + 100);
new newPointParameter(42,24);
}
class Point {
private double x = 1;
private double y = 1;
public double getx() {
return x;
}
public double gety() {
return y;
}
public void changePoint(double newx, double newy) {
x = newx;
y = newy;
}
public void newPointParameters(double x1, double y1) {
this.x = x1;
this.y = y1;
}
public void newPoint() {
this.x = 10;
this.y = 10;
}
public double distanceFrom(double x2, double y2) {
double x3 = x2 - this.x;
double y3 = y2 - this.y;
double sqaureadd = (y3 * y3) + (x3 * x3);
double distance = Math.sqrt(sqaureadd);
return distance;
}
}
}
So, currently, neither newPointParameters nor newPoint are constructors. Rather, they are just methods. To make them into constructors, they need to share the same name as the class the construct
class Point {
private double x = 1;
private double y = 1;
public Point() {
this.x = 10;
this.y = 10;
}
public Point(double x, double y) {
this.x = x;
this.y = y;
}
Then, when you want to create a new point, you simply do the following
For a default point
public class Lab4ex1 {
public static void main(String[] args) {
System.out.println("" + 100);
//this will create a new Point object, and call the Point() constructor
Point point = new Point();
}
For the Point with parameters
public class Lab4ex1 {
public static void main(String[] args) {
System.out.println("" + 100);
//this will create a new Point object, and call the
//Point(double x, double y) constructor
Point point = new Point(10.0, 10.0);
}
It should be
public static void main(String[] args) {
System.out.println("" + 100);
Point p = new Point();
p.newPointParameter(42,24);
}
newPointParameters is not a constructor. I think this is what you are looking to do:
public Point(double x1, double y1) {
x = x1;
y = y1;
}
Then you can create a Point object in your main class using this constructor:
Point p = new Point(42, 24);
It looks like you also intended for newPoint() to be a constructor, so it should look like this:
public Point() {
x = 10;
y = 10;
}

Smooth velocity based movement with predictable duration

Anyone has a good algorithm for how to get a smooth but predictable movement from point a -> b in 2D in any language?
I need to have one function setting the velocity each frame:
function GetVel(current_pos : Vector2, dest_pos : Vector2, current_vel : Vector2)
{
var new_vel : Vector2d;
.......
return new_vel;
}
and a corresponding:
function GetDestTime(current_pos : Vector2, dest_pos : Vector2, current_vel : Vector2 )
{
var duration : float;
.......
return duration;
}
Simply using acceleration leads up to lots of sliding so some good smoothDamp algorithm that can be predicted the exact dest time is what I need.
Any ideas?
Let's assume v(0) = 0 and v(T) = 0 and, v(t) is a quadratic function which the maximum value at t = T/2.
Accordingly, we can assume the form,
Since the point moves L within T seconds, integrating v(t) from 0 to T must give L. So, we can get another equation,
Solving these equations gives,
Using these a and b, you can compute the current velocity.
It is rather long, but I made a Java toy to realize this. Please check it!
import java.awt.*;
import javax.swing.*;
public class MovePoint extends Canvas implements Runnable {
public static void main(String... args) {
Thread thread = new Thread(new MovePoint());
thread.start();
}
private static final int WIDTH = 500;
private static final int HEIGHT = 500;
public MovePoint() {
super();
this.setBackground(Color.WHITE);
this.setForeground(Color.BLACK);
this.setSize(WIDTH, HEIGHT);
JFrame f = new JFrame("Move Point");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(this);
f.pack();
f.setVisible(true);
}
private Point V;
private Point S = new Point(50, 50);
private Point E = new Point(450, 450);
private double duration = 5.0;
private double dt = 0.03;
private Image buffer;
private Graphics gBuf;
public void run() {
double t = 0.0;
V = S.copy();
while (t < duration) {
V = Point.add(V, calcVelocity(V, S, E, t, duration).scale(dt));
t += dt;
repaint();
try {
Thread.sleep(30);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.exit(0);
}
public void paint(Graphics g) {
if (gBuf == null) {
buffer = createImage(WIDTH, HEIGHT);
gBuf = buffer.getGraphics();
}
gBuf.setColor(Color.WHITE);
gBuf.fillRect(0, 0, WIDTH, HEIGHT);
gBuf.setColor(Color.BLACK);
gBuf.fillOval((int)(V.x - 5), (int)(V.y - 5), 11, 11);
g.drawImage(buffer, 0, 0, this);
}
public void update(Graphics g) {
paint(g);
}
public Point calcVelocity(Point current, Point start, Point goal, double t, double T) {
double L = Point.distance(start, goal);
double a = -6.0 / (T * T * T);
double b = 3.0 / (2.0 * T);
double s = (t - 0.5 * T);
double v = a * s * s + b;
return Point.subtract(goal, start).scale(v);
}
}
class Point {
public double x;
public double y;
public Point(double x, double y) {
this.x = x;
this.y = y;
}
public Point copy() {
return new Point(x, y);
}
public static double distance(Point p, Point q) {
double dx = p.x - q.x;
double dy = p.y - q.y;
return Math.sqrt(dx * dx + dy * dy);
}
public static Point add(Point p, Point q) {
return new Point(p.x + q.x, p.y + q.y);
}
public static Point subtract(Point p, Point q) {
return new Point(p.x - q.x, p.y - q.y);
}
public Point scale(double s) {
return new Point(x * s, y * s);
}
}

Static method to turn shapes red

so i made this program and i am trying to add in my testing file a static method that turns a random array of shapes all "red".
public abstract class Shape
shape class
public abstract class Shape
{
private String color;
public Shape() { color = "white";}
public String getColor() { return color;}
public void setColor(String c) { color = c; }
public abstract double area();
public abstract double perimeter();
public abstract void display();
}
circle class
public class Circle extends Shape {
private double radius;
public Circle( double r)
{
super();
radius = r;
}
public double getRadius()
{ return radius; }
//Implement area, perimeter and display
public double area()
{
return Math.PI * radius* radius;
}
public double perimeter()
{
return 2* Math.PI *radius;
}
//Circle class - continued
public void display()
{
System.out.println( this);
}
public String toString()
{
return "Circle: radius:" + radius
+ "\tColor: " + getColor();
}
}
my main class for testing
public class TestingShapes {
public static double sumArea( Shape[] b)
{
double sum = 0.0;
for( int k = 0; k < b.length; k++)
{
sum = sum + b[k].area();
}
return sum;
}
public static void printArray( Shape[] b)
{
for (Shape u: b)
System.out.println(u + "\tArearea " + u.area());
System.out.println();
}
public static void main( String[] args)
{
Shape[] list = new Shape[20]; //Not creating Shapes
for ( int k = 0 ; k < list.length; k++)
{
double z = Math.random();
if( z < 0.33 )
list[k] = new Circle(1 + Math.random() * 10);
else if(z<0.66)
list[k] = new Rectangle ( 3*(k+1), 4*(k+1), 5*(k+1),6*(k+1));
else
list[k] = new Triangle ( 3*(k+1), 4*(k+1), 5*(k+1));
}
printArray(list);
System.out.println();
double sum = sumArea(list);
System.out.println("Sum of List Area: " + sum);
}
To turn some shapes randomly red, you would create a method accepting the array of shapes, and loop over it. You can use Math.random() to get a random floating point number between 0 and 1. To turn, say, 20% of shapes red, you would just compare Math.random() to 20%: if (Math.random() < 0.2) { call the shape's setColor method with "red" }. Since arrays/collections in Java are passed by reference, you don't need to return anything from the method, it will modify the copy that the caller owns.

Categories

Resources