I am relatively new to the land of Java and google has had no help, this may be a common rookie mistake but I am not exactly sure how to go about debugging. My program is relatively small (1 class) and I figured it couldn't hurt to see if someone could figure out my idiocy. And btw this programs solves quadratics, just for reference <3
My errors (all errors originate from their respective if statements):
error: method quadneggative in class QuadraticFormula cannot be
applied to given types;
error: method quadpositive in class QuadraticFormula cannot be applied to given types;
error: method diszero in class QuadraticFormula cannot be applied to given types;
My program:
import java.lang.*;
import java.util.*;
public class QuadraticFormula {
public QuadraticFormula() {
}
public static void main(String[] args) {
runnermethod();
}
public static double scannermethod()
{
Scanner keyboardInput = new Scanner(System.in);
System.out.print("Please input a value:");
double val = keyboardInput.nextDouble();
return val;
}
public static double dis(double aval, double bval, double cval)
{
double a = aval;
double b = bval;
double c = cval;
double dis = Math.pow(b,2)-(4*a*c);
return dis;
}
public static void diszero(double aval, double bval)
{
double a = aval;
double b = bval;
double quadzero = (-1*b)/(2*a);
System.out.print("Your real solution is: x ="+quadzero);
}
public static void quadpositive(double aval, double bval, double dis)
{
double d = dis;
double a = aval;
double b = bval;
double quadsqr = Math.sqrt(d);
double quadpostop = (-1*b)+quadsqr;
double quadnegtop = (-1*b)-quadsqr;
double quadposall = quadpostop/(2*a);
double quadnegall = quadnegtop/(2*a);
System.out.print("Your real solutions are: x ="+quadposall+","+quadnegall+"i");
}
public static void quadneggative(double aval, double bval, double dis)
{
double d = dis;
double a = aval;
double b = bval;
double quadsqr = Math.sqrt(d*(-1));
double quadpostop = (-1*b)+quadsqr;
double quadnegtop = (-1*b)-quadsqr;
double quadposall = quadpostop/(2*a);
double quadnegall = quadnegtop/(2*a);
System.out.print("Your imaginary solutions are: x ="+quadposall+"i,"+quadnegall+"i");
}
public static void runnermethod()
{
scannermethod();
double a = scannermethod();
double b = scannermethod();
double c = scannermethod();
dis(a, b, c);
double disc = dis(a, b, c);
if(disc < 0)
{
quadneggative();
}
if(disc > 0)
{
quadpositive();
}
if(disc == 0)
{
diszero();
}
}
}
you forgot to add the parameters to the methods
it should be like:
if(disc < 0) {
quadneggative(a,b,disc);
}
if(disc > 0) {
quadpositive(a,b,disc);
}
if(disc == 0) {
diszero(a,b);
}
I already have the class I need to implement in to my code. The instructions are: Code a testing program/class. This should construct or instantiate objects of the class you coded in step #1. Your testing program should call every method to make sure they work. You should construct at least two objects – one with the default constructor and one with the “other” constructor. For the second scenario, ask the user what values for (radius and) height. You may use any input and output that you want for this.
This is what I have so far and I'm stuck:
public class Cube
{
private double height;
public Cube(){
height = 1.0;
}
public Cube(double h){
height = h;
}
public double getHeight(){
return height;
}
public void setHeight(double h){
height = h;
}
public double calcVolume() {
return height*height*height;
}
public double calcSurface(){
return height*height*6;
}
public String toString(){
return this.toString();
}
public boolean equals(Cube c){
return (c.getHeight() == this.height);
}
}
import java.util.*
public class TestTheCube
{
public static void main(String[] args)
{
Cube cube1 = new Cube();
Scanner kb = new Scanner(System.in);
System.out.print("Enter a height as a positive number");
double height = kb.nextDouble();
Cube cube2 = new Cube(height);
System.out.println(
}
}
I've invoked calcVolume() of cube1 and cube2.
Cube cube1 = new Cube();
Scanner kb = new Scanner(System.in);
System.out.print("Enter a height as a positive number");
double height = kb.nextDouble();
Cube cube2 = new Cube(height);
System.out.println("Cube 1's volume = "+cube1.calcVolume());
System.out.println("Cube 2's volume = "+cube2.calcVolume());
.....//repeat for every instance method you have.
I'm creating a class that will create a Pizza object. It takes into account its size (diameter in inches), the number of slices, the cost of the pie, and the type of pizza.
This is my first time doing such a thing, so I've run into some snafus.
Here is the code for the class Pizza:
class Pizza
{
//instance variables
int size;
int slices;
int pieCost;
String typeOfPizza;
//constructors
Pizza (String typeOfPizza)
{
System.out.println (typeOfPizza);
}
Pizza ()
{
System.out.println ("pizza");
}
Pizza (int s, int sl, int c)
{
size = s;
slices = sl;
pieCost = c;
typeOfPizza = "????";
}
Pizza (String name, int s, int sl, int c)
{
typeOfPizza = name;
size = s;
slices = sl;
pieCost = c;
}
//behavior
double areaPerSlice(int size, int slices)
{
double wholeArea = Math.PI * Math.pow ((size/2), 2);
double sliceArea = wholeArea/slices;
return sliceArea;
}
double costPerSlice (double pieCost, int slices)
{
double sliceCost = pieCost/slices;
return sliceCost;
}
double costPerSquareInch (double sliceCost, double sliceArea)
{
double costPerSquareInch = sliceCost/sliceArea;
}
String getName(String name)
{
String typeOfPizza = name;
return typeOfPizza;
}
}
Here is the code for the main method that calls on the Pizza class:
class PizzaTest
{
public static void main (String [] args)
{
String typeOfPizza = "Cheese";
int size = 10; //in inches, referring to the diameter of the pizza
int numberOfSlices = 10; //number of slices
int costOfPie = 20;
Pizza myPizza = new Pizza (typeOfPizza, size, numberOfSlices, costOfPie);
System.out.printf ("Your %s pizza has %.2f square inches per slice.\n", myPizza.getName(),
myPizza.areaPerSlice() );
System.out.printf ("One slice costs $%.2f, which comes to $%.3f per square inch.\n",
myPizza.costPerSlice(), myPizza.costPerSquareInch());
}
}
Essentially, the output should print the following:
Your Pepperoni pizza has 20.11 square inches per slice.
One slice costs $1.05, which comes to $0.052 per square inch.
The values can be ignored, they're from an example with different parameters. When I go to compile this program, I get the following errors:
getName(java.lang.String) in Pizza cannot be applied to ()
System.out.printf ("Your %s pizza has %.2f square inches per slice.\n", myPizza.getName(),
^
PizzaTest.java:20: areaPerSlice(int,int) in Pizza cannot be applied to ()
myPizza.areaPerSlice() );
^
PizzaTest.java:23: costPerSlice(double,int) in Pizza cannot be applied to ()
myPizza.costPerSlice(), myPizza.costPerSquareInch());
^
PizzaTest.java:23: costPerSquareInch(double,double) in Pizza cannot be applied to ()
myPizza.costPerSlice(), myPizza.costPerSquareInch());
Any input as to how I can fix this? Thanks for helping out a beginning programmer!
Change
String getName(String name)
{
String typeOfPizza = name;
return typeOfPizza;
}
to
String getName()
{
return typeOfPizza;
}
and
double costPerSquareInch (double sliceCost, double sliceArea){
double costPerSquareInch = sliceCost/sliceArea;
}
to
double costPerSquareInch (double sliceCost, double sliceArea){
return costPerSquareInch = sliceCost/sliceArea;
}
and
System.out.printf ("Your %s pizza has %.2f square inches per slice.\n", myPizza.getName(),
myPizza.areaPerSlice() );
System.out.printf ("One slice costs $%.2f, which comes to $%.3f per square inch.\n",
myPizza.costPerSlice(costOfPie,numberOfSlices), myPizza.costPerSquareInch(sliceCost,sliceArea));
to
System.out.printf ("Your %s pizza has %.2f square inches per slice.\n", myPizza.getName(),
myPizza.areaPerSlice(size, numberOfSlices) );
System.out.printf ("One slice costs $%.2f, which comes to $%.3f per square inch.\n",
myPizza.costPerSlice(costOfPie,numberOfSlices), myPizza.costPerSquareInch(sliceCost,sliceArea));
Your function takes a non optional string.
String getName(String name)
{
String typeOfPizza = name;
return typeOfPizza;
}
Should be two functions
String getName() { // needed a string before.
return typeOfPizza;
}
void setName(String name) {
typeOfPizza = name;
}
Also, you should probably call that field name or those method(s) should be getTypeOfPizza and setTypeOfPizza.
the errors say it all...
double areaPerSlice(int size, int slices)
{
double wholeArea = Math.PI * Math.pow ((size/2), 2);
double sliceArea = wholeArea/slices;
return sliceArea;
}
System.out.printf ("Your %s pizza has %.2f square inches per slice.\n", myPizza.getName(),
myPizza.areaPerSlice() );
in the method definition, you are taking 2 args... but while calling you are using 0 arguments... THIS APPLIES FOR OTHER ERRORS ALSO.
problem
Your function is asking for a parameter.As you don't have function getName with single parameter
Solution
You have to define a method like
String getName( )
{
return typeOfPizza;
}
Same you have to do for these calls also.
myPizza.areaPerSlice()
myPizza.costPerSlice()
myPizza.costPerSquareInch()
I don't think your Pizza class will compile.
double costPerSquareInch (double sliceCost, double sliceArea){
double costPerSquareInch = sliceCost/sliceArea;
// missing return.
}
First make that one correct.
Then
myPizza.getName() // need input argument as String
Then this should as
myPizza.getName("inputString");
Also other two has same input argument issues.
myPizza.areaPerSlice()// two int input arguments.
Corrected as
myPizza.areaPerSlice(1,2);
Next one
myPizza.costPerSlice();// double and int input arguments.
correct as
myPizza.costPerSlice(2.0,2);
Finally
myPizza.costPerSquareInch() // two double input arguments.
Corrected as
myPizza.costPerSquareInch(2.0,1.0) ;
public class Pizza
{
//instance variables
pirvate int size;
private int slices;
private int pieCost;
private String typeOfPizza;
//constructors
public Pizza (String typeOfPizza)
{
System.out.println (typeOfPizza);
}
public Pizza ()
{
System.out.println ("pizza");
}
public Pizza (int size, int slices, int pieCost)
{
this.size = size;
this.slices = slices;
this.pieCost = pieCost;
typeOfPizza = "????";
}
public Pizza (String typeOfPizza, int size, int slices, int pieCost)
{
this.typeOfPizza = typeOfPizza
this.size = size;
this.slices = slices;
this.pieCost = pieCost;
}
//behavior
public double areaPerSlice(int size, int slices)
{
double wholeArea = Math.PI * Math.pow ((size/2), 2);
double sliceArea = wholeArea/slices;
return sliceArea;
}
public double costPerSlice (double pieCost, int slices)
{
double sliceCost = pieCost/slices;
return sliceCost;
}
public double costPerSquareInch (double sliceCost, double sliceArea)
{
double costPerSquareInch = sliceCost/sliceArea;
}
public String getName(String name)
{
String typeOfPizza = name;
return typeOfPizza;
}
}
class PizzaTest
{
public static void main (String [] args)
{
String typeOfPizza = "Cheese";
int size = 10; //in inches, referring to the diameter of the pizza
int numberOfSlices = 10; //number of slices
int costOfPie = 20;
Pizza myPizza = new Pizza (typeOfPizza, size, numberOfSlices, costOfPie);
System.Out.Printf ("Your"+myPizza.getName()+" pizza has "+myPizza.areaPerSlice() +".2f square inches per slice.\n");
System.Out.Printf ("One slice costs "+myPizza.costPerSlice()+".2f, which comes to "+myPizza.costPerSquareInch()+".3f per square inch.\n");
}
}
Hope this will help you.
I have a main class to run a BMI calculator class which calculates BMI info (body mass index) with fields for name, age, sex, height, and weight.
There is also a WaistToHip calculator class to calculate waist-to-hip ratio with fields
waist and hip.
However, when I wanted to create a
BodyFat calculator I need the height and waist from both classes.
How am I suppose to call these in my body fat calculator class for my formula?
public class body_fat_calculation {
private double neck;
private double CBF;
waist_to_hip_ratio waist;
bmiCalculator height;
public body_fat_calculation(double neck) {
super();
this.neck = neck;
}
public double getCBF() {
return CBF;
}
public void setCBF(double cBF) {
CBF = cBF;
}
public double getNeck() {
return neck;
}
public void setNeck(double neck) {
this.neck = neck;
}
public double Round(double Rval, int Rpl){
double p = Math.pow(10, Rpl);
Rval=Rval*p;
double tmp = Math.round(Rval);
return tmp/p;
}
public void calculateWTHR(){
CBF= Round((495/(1.0324 - 0.19077 * Math.log10((waist)-(neck)) + 0.15456 * Math.log10(height)) - 450),2);
}
}
Why don't you do something like this? If you notice I have added two parameters to the BodyFatCalculator class - waist and height.
public class Main {
public static void main(string[] args){
// I assume you will want to use a scanner to get user input to set these variables dynamically
// for the sake of the example, I have set them myself.
double height = 1.82; // meters
double weight = 170.0;
double waist = 35.0;
double hip = 40.0;
double neck = 7.1;
String name = "Dave";
String sex = "M";
int age = 20;
// create new BMI Calculator to figure out body mass index
BMICalculator bmiCalc = new BMICalculator(name, age, sex, height, weight);
double bmi = bmiCalc.calculateBmi(); // calculate body mass index
WaistToHipCalculator waistHipCalc = new WaistToHipCalculator(waist, hip);
double whr = waistHipCalc.calculateWhr(); // calculate waist to hip ratio
BodyFatCalculator bfCalc = new BodyFatCalculator(neck, height, waist);
double bf = bfCalc.calculateBf(); // calculate body fat
// print results
}
}
Classes are more nouns. A verb should be a method. With a class called BodyFatCalculation I think you're trying too hard to use a lot of classes. Just have a Body (noun) class with calculateBodyMassIndex and calculateBodyFat (verbs) as methods. You could create a Dimensions class with height and waist in it (with getters and setters as needed) and keep an instance of that inside the Body class, but that's overkill here.
class TestTax {
public static void main (String[] args){
NJTax t = new NJTax();
t.grossIncome= 50000;
t.dependents= 2;
t.state= "NJ";
double yourTax = t.calcTax();
double totalTax = t.adjustForStudents(yourTax);
System.out.println("Your tax is " + yourTax);
}
}
class tax {
double grossIncome;
String state;
int dependents;
public double calcTax(){
double stateTax = 0;
if(grossIncome < 30000){
stateTax = grossIncome * 0.05;
}
else{
stateTax = grossIncome * 0.06;
}
return stateTax;
}
public void printAnnualTaxReturn(){
// code goes here
}
}
public class NJTax extends tax{
double adjustForStudents (double stateTax){
double adjustedTax = stateTax - 500;
return adjustedTax;
public double calcTax(){
}
}
}
I am having trouble with the Lesson requirement to:
"Change the functionality of calcTax( ) by overriding it in NJTax. The new version of calcTax( ) should lower the tax by $500 before returning the value."
How is this accomplished. I just have safaribooksonline and no videos for the solution.
http://download.oracle.com/javase/tutorial/java/IandI/override.html
Class names should start with a capital letter as well. Since I'm not exactly sure what you wanted regarding functionality, here's just an example. Super refers to the parent class, in this case being tax. Thus, NJTax's calcTax() method returns tax.calcTax() - 500. You also may want to look into using the #Override annotation as a way of making it clear that a method is being overridden and to provide a compile-time check.
public class NJTax extends tax {
public double adjustForStudents (double stateTax) {
double adjustedTax = stateTax - 500;
return adjustedTax;
}
public double calcTax() {
return super.calcTax() - 500;
}
}
public class NJTax extends tax{
double adjustForStudents (double stateTax){
double adjustedTax = stateTax - 500;
return adjustedTax;
}
public double calcTax(){
double stateTax = super.calcTax();
return this.adjustforStudents(stateTax);
}
}
hint: return the same value as tax.calcTax() minus $500.
For overriding you base class (Tax) implementation of calcTax, just add your own implementation of calcTax in NJTax. This could be as simple as
public double calcTax(){
return super.calcTax() -500;
}