So I need to implement a getCurrentVolumeInCCs method for the vessel class, however I am a little confused as to how to do it. Also I am getting the correct values for the volume in both the Glass and Cup classes but when I call any of the other methods in the Vessel class these values do not get carried over. I'm guessing its because they are stored in the subclass and cannot be used in the base class, am I right?
Vessel Class (abstract)
package containers;
public abstract class Vessel {
private double volume;
private double currentVolume;
public Vessel() {
volume = 0;
currentVolume = 0;
}
abstract double getVolumeInCC();
public double getCurrentContents() {
return currentVolume;
}
public double drink(double amountToDrink) {
double volumeLeft;
System.out.println("you drank " + amountToDrink + " CCs");
volumeLeft = volume - amountToDrink;
return volumeLeft;
}
public double fill(double amountPoured) {
double volumeAdded = amountPoured;
double volumeSpilled = 0;
System.out.println("you poured " + amountPoured + " CCs");
if(volumeAdded > volume) {
volumeSpilled = amountPoured - volume;
System.out.println("you spilled " + volumeSpilled);
}
return volumeAdded;
}
}
Glass Class
package containers;
public class Glass extends Vessel{
private double volume;
private double radiusBase;
private double height;
public Glass() {}
public Glass(double r, double h) {
this.radiusBase = r;
this.height = h;
}
#Override
double getVolumeInCC() {
volume = (Math.PI)*(Math.pow(radiusBase, 2))*height;
return volume;
}
}
Cup Class
package containers;
public class Cup extends Vessel {
private double volume;
private double radiusLip;
public Cup(double r) {
volume = 0;
this.radiusLip = r;
}
#Override
double getVolumeInCC() {
volume = (0.5)*(Math.PI)*(Math.pow(radiusLip, 3))*(4/3);
return volume;
}
}
You are shadowing volume from Vessel in your subclasses because it's a private field. I believe that you wanted to make volume and currentVolume protected in Vessel,
protected double volume;
protected double currentVolume;
Also, you should remove private double volume from the sub-classes (so you don't shadow the field they now inherit from Vessel).
Related
Hi I am trying to write this piece of code about physics but have a problem.
In fact two problems:
1.the formula for gravitation its throwing a error about using the field .class
2.I want to declare the types of individual variables in a element
I think its clearer if I show the source code:
public class Main
{
public static void main(String[] args) {
}
abstract class Gravitation{
public abstract int Gravitation(int mass[],int dist,int force);
}
class PhySyst{
public int mass;
public int wt;
public float smallG = 9.8f;
//this is the posible value of charge
enum Charge{Positive,Negative,PositiveCoulumb,NegativeCoulumb,IsNegative,IsPositive};
}
class Earth extends Gravitation{
//stores acceleration due to gravity
public float smallG = 9.8f;
//the raduis of earth is needed in the physics formula for distance
public float GravityPressure = 6.67f;
public float raduisofEarth = 6371f;
//this finds the gravation using newtons principles
public int Gravitation(int mass[],int dist,int force)
{
force = (GravityPressure * mass[])/(dist * dist);
return force;
}
}
}
the error is this:
Main.java:32: error: '.class' expected
force = (GravityPressure * mass[])/(dist * dist);
^
1 error
Thanks for reading :)
Couple of Fixes here:
Constants should be static final (#constant).
Gm1m2/r2, formula so rather than array you should pass 2 input params (m1 and m2).
As multiplication is operation for number not array (mass[]).
Enum should be defined in public class or in static class.
As the force can be float, so change the return type to float from current int.
#JsonIgnoreProperties(ignoreUnknown = true)
class Category {
abstract class Gravitation {
public abstract float getGravitationalForce(int mass1, int mass2, int dist);
}
//this is the possible value of charge
enum Charge {
Positive, Negative, PositiveCoulumb, NegativeCoulumb, IsNegative, IsPositive
};
class PhySyst {
public int mass;
public int wt;
public float smallG = 9.8f;
}
class Earth extends Gravitation {
//stores acceleration due to gravity
public static final float SMALL_G = 9.8f; #constant
//the radius of earth is needed in the physics formula for distance
public static final float GRAVITATIONAL_PRESSURE = 6.67f; #constant
public static final float RADIUS_OF_EARTH = 6371f; #constant
//this finds the gravitation using newtons principles
public float getGravitationalForce(int mass1, int mass2, int dist) {
return (GRAVITATIONAL_PRESSURE * mass1 * mass2) / (dist * dist);
}
}
}
I made a java program that compares the distance between 2 objects
I managed to figure out how to solve it when I make a class with 2 parameters and compare these with the formula seen below;
public class RasterServices {
public static double distance (SimpleRasterElement a, SimpleRasterElement b) {
double d;
d = Math.sqrt(((b.x-a.x)*(b.x-a.x)) + ((b.y-a.y)*(b.y-a.y)));
return d;
}
public class SimpleRasterElement {
public int id;
public double x;
public double y;
public double height;
}
public class SimpleRasterElementTest {
public static void main (String[] args)
{
SimpleRasterElement a, b ; // Deklarera variabeln
a = new SimpleRasterElement (); // Skapa en instans (med ’new’),
b = new SimpleRasterElement ();
// Tilldela variablerna i ’a’ värden:
a.id = 1;
a.x = 6.0;
a.y = 8.0;
a.height = 10.5;
// Tilldela variablerna i ’b’ värden:
b.id = 1;
b.x = 9.0;
b.y = 12.0;
b.height = 15.5;
System.out.println (RasterServices.distance(a,b));
}
}
I can then test this via using my test program RasterServices.distance(a,b)
But now I want to make my variables private, use getters and create the distance() method within the RasterElement-class, and now I'm hardstuck.
public class RasterElementTest {
public static void main(String[] args) {
RasterElement re_a = new RasterElement(1, 6.0, 8.0, 10.5);
RasterElement re_b = new RasterElement(1, 9.0, 12.0, 15.5);
double d = re_a.distance(re_b);
System.out.println(d);
}
}
asd
public class RasterElement {
private final int id;
private final double x;
private final double y;
private final double height;
public RasterElement (int id_nr, double x_val, double y_val, double height_val) {
id = id_nr;
x = x_val;
y = y_val;
height = height_val;
}
public int getId () {
return id;
}
public double getX () {
return x;
}
public double getY () {
return y;
}
public double getHeight () {
return height;
}
public double distance (RasterElement a) {
double d;
d = Math.sqrt(((b.getX()-a.getX())*(b.getX()-a.getX())) + ((b.getY()-a.getY())*b.getY()-a.getY()));
return d;
}
}
But here in distance() i'm only allowed ONE parameter, can someone please explain to me how I can compare two elements/objects when I'm only allowed one parameter in the function?
(By using re_a.distance(re_b); in my test-code)
Thanks, sorry for the long post B-)
(how do I get the b-value into the equation in the method distance in the class RasterElement..?)
Change b to this. Also, you can eliminate d and return directly. Like,
public double distance (RasterElement a) {
return Math.sqrt(((this.getX()-a.getX())*(this.getX()-a.getX()))
+ ((this.getY()-a.getY())*this.getY()-a.getY()));
}
How should I pass the "type" parameter of a constructor in an if/else statement? For eg - cal(2,2,0,rectangle). So if the type=rectangle then calculate area of a rectangle. If type=circle, calculate the area of a circle.
I am using a single constructor. My issue is that I know the logic but I can't write it in syntax. I am using Java or Apex.
I want to use if-else statement. How should I pass the type parameter in the code?
My program is like this -
if "type"=square, the compiler will call calculate area of the square.
if "type"=circle, the compiler will call calculate area of the circle.
public class Area {
private String type;
private Integer length;
private Integer breadth;
private Integer height;
private Integer area;
public void setType(String t){
type=t;
}
public void setLength(Integer l){
length=l;
}
public void setbreadth(Integer b){
breadth=b;
}
public void setheight(Integer h){
height=h;
}
/* public void setArea(Integer a){
area=a;
} */
public Integer getLength(){
return length;
}
public Integer getbreadth(){
return breadth;
}
public Integer getheight(){
return height;
}
public string gettype(){
return type;
}
public Integer AreaRectangle(){
return area=length*breadth;
}
public Integer AreaSquare(){
return area=length*length;
}
public integer AreaTriangle(){
return area=1/2 *(breadth*height);
}
public Area(){ // default constructor
length=9;
breadth=2;
height=7;
}
public Area(String t,Integer l ,Integer b,Integer h ){ // parameterised constructor
type=t;
length=l;
breadth=b;
height=h;
}
}
You don't. You create an abstract class called shape.
public abstract class Shape {
abstract double area();
}
And then two other classes that extend Shape and each provides the proper implementation
public class Square extends Shape {
private int side;
public Square(int side) {
this.side = side;
}
public double area() {
return (double) side * side;
}
}
Now at the place you want to call it:
Shape shape = new Square(5);
double area = shape.area();
Int radius = 4;
shape = new Circle(radius);
double circle area = shape.area();
In Jcreator, there are 12 errors here, I don't know how to fix it. It says "illegal start of expr....".
If I change something, suddenly, 50 errors more.
public class Practica_figura {
Class Figura() {
private float base;
private float altura;
private float radio;
}
public void asignar(float ba, float al, float ra) {
base = ba;
altura = al;
radio = ra;
}
class Cuadrado extends Figura()
{
private float base;
private float altura;
public void calcular_area(float b, float a) {
float res = base * altura;
}
public void calcular_perimetro(float a) {
float resp = 4 * a;
}
public void rareac() {
return area;
}
public void rperic() {
return perimetro;
}
}
class Triangulo extends Figura()
{
private float base;
private float altura;
private float la;
private float lb;
private float lc;
public void asignar(float a, float b, float c) {
la = a;
lb = b;
lc = c;
}
public void calcular_area(float b, float a) {
float res = (base * altura) / 2;
}
public void calcular_perimetro(float a) {
float resp = 4 * a;
}
public void rareat() {
return area;
}
public void rperit() {
return perimetro;
}
}
public static void main(String[] args) {
// TODO code application logic here
float base = 0, altura = 0, radio = 0;
JOptionPane.showMessageDialog(null, "Programa para calcular área y perímetro");
}
}
To have a correct class definition change
public class Practica_figura { Class Figura(){
to
public class Figura{
I don't really understand what you were trying to do, if what you wanted was a constructor then you would have to add:
public Figura(){
}
First, the class is not quite defined properly.
public class Practica_figura
{
private float base;
private float altura;
private float radio;
public Practica_figura()
{
}
public void asignar(float ba, float al, float ra)
{
base = ba;
altura = al;
radio = ra;
}
//edits assuming you want inner classes
class Figura
{
private float base, altura, radio;
// you will need getters/setters for the variables
}
class Cuadrado extends Figura
{
// insert class logic here
}
class Triangulo extends Figura
{
// insert class logic here
}
}
Second, you are shadowing variables in your child classes, and this approach may make things confusing.
I need to inherit an instance method from the superclass but I am stuck.
public class Pay
private float hours;
private float rate;
private int hrsStr;
float gross;
double tax;
public void calc_Payroll()
{
if (hrsStr != 0)
gross = hrsStr + ((hours - hrsStr) * 1.33f) * rate;
else
gross = hours * rate;
}
public void tax(double a)
{
if (gross <= 399.99)
tax = .92;
else
if (gross <= 899.99)
tax = .88;
else
tax = .84;
}
this is the part of the super class, i need have the same method signature(???) was well as invoke tax(double a) and calc_payroll()
this is what i had for the sub class but it wasn't working.
public class Payroll extends Pay
{
float net;
void calc_payroll()
{
float finaltax = (float) tax;
net = gross * finaltax;
}
}
Your void calc_payroll() in your sub class does not either override or invoke the super class method.
If you want to override the method in your base class, the method signature must be the same. Use the #override annotation for clarity.
#override
public void calc_Payroll(float a, float b, int c, float d)
{
}
If your new method has a different signature, then you can call the super class method using....
void calc_payroll()
{
super.calc_Payroll( ... );
}
Depends what behaviour you require in your new method and if you want to invoke the behaviour in the super class method.