again I have a computer science question. I am doing a lab for AP computer science in which we are given 2 skeleton codes and a tester class and a interface class. Our job is to make the tester run with no errors. The code I got was a triangle math code (side length, perimeter, area, that sort of thing). I made it so it would all run, but one of the tests keeps failing. I dont know why, please help me.
Here is the triangle main code:
import java.util.Scanner;
import java.lang.Math.*;
import java.lang.*;
public class Triangle implements TestableTriangle
{
private int sideA, sideB, sideC;
private double perimeter;
private double theArea;
public Triangle()
{
setSides(0,0,0);
perimeter=0;
theArea=0;
}
public Triangle(int a, int b, int c)
{
sideA=a;
sideB=b;
sideC=c;
}
public void setSides(int a, int b, int c)
{
sideA=a;
sideB=b;
sideC=c;
}
public void calcPerimeter( )
{
perimeter=(sideA+sideB+sideC);
}
public void calcArea( )
{
double s;
s=(perimeter/2);
theArea=(s*(s-a)*(s-b)*(s-c));
}
public void print( )
{
System.out.println("\n\n");
System.out.println(sideA+" "+sideB+" "+sideC+"\n");
}
public int getSideA()
{
return sideA;
}
public int getSideB()
{
return sideB;
}
public int getSideC()
{
return sideC;
}
public double getPerimeter()
{
return perimeter;
}
public double getTheArea()
{
return theArea;
}
}
Here is the lab code. This class is used to test Triangle
import java.util.Scanner;
import java.lang.Math.*;
public class Lab03a //this class is used to test Triangle
{
public static void main( String[] args )
{
Scanner keyboard = new Scanner(System.in);
//ask for user input
System.out.print("Enter side A :: ");
int a = keyboard.nextInt();
System.out.print("Enter side B :: ");
int b = keyboard.nextInt();
System.out.print("Enter side C :: ");
int c = keyboard.nextInt();
Triangle test = new Triangle(a, b, c);
test.calcPerimeter();
test.calcArea();
test.print();
//ask for user input
System.out.print("Enter side A :: ");
a = keyboard.nextInt();
System.out.print("Enter side B :: ");
b = keyboard.nextInt();
System.out.print("Enter side C :: ");
c = keyboard.nextInt();
test.setSides(a,b,c);
test.calcPerimeter();
test.calcArea();
test.print();
//add one more input section
System.out.print("Enter side A :: ");
a = keyboard.nextInt();
System.out.print("Enter side B :: ");
b = keyboard.nextInt();
System.out.print("Enter side C :: ");
c = keyboard.nextInt();
test.setSides(a,b,c);
test.calcPerimeter();
test.calcArea();
test.print();
}
}
Here is the interface code:
public interface TestableTriangle
{
public void setSides(int a, int b, int c);
public void calcPerimeter( );
public void calcArea( );
public void print( );
public int getSideA();
public int getSideB();
public int getSideC();
public double getPerimeter();
public double getTheArea();
}
Here is the tester code (and below it is the error message it shows):
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class TriangleTest
{
private Triangle triangle1;
/**
* Default constructor for test class TriangleTest
*/
public TriangleTest()
{
}
/**
* Sets up the test fixture.
*
* Called before every test case method.
*/
#Before
public void setUp()
{
triangle1 = new Triangle(37, 38, 39);
}
/**
* Tears down the test fixture.
*
* Called after every test case method.
*/
#After
public void tearDown()
{
}
#Test
public void testConstructorAndSides()
{
//Triangle triangle1 = new Triangle(35, 36, 37);
assertEquals(37, triangle1.getSideA());
assertEquals(38, triangle1.getSideB());
assertEquals(39, triangle1.getSideC());
}
#Test
public void testConstructorAndPerimeter()
{
//Triangle triangle1 = new Triangle(36, 37, 38);
assertEquals(0.0, triangle1.getPerimeter(), 0.1);
triangle1.calcPerimeter();
assertEquals(114.0, triangle1.getPerimeter(), 0.1);
}
#Test
public void testSetSidesAndSides()
{
//Triangle triangle1 = new Triangle(37, 38, 39);
triangle1.setSides(10, 15, 16);
assertEquals(10, triangle1.getSideA());
assertEquals(15, triangle1.getSideB());
assertEquals(16, triangle1.getSideC());
}
#Test
public void testSetSidesAndPerimeter()
{
Triangle triangle2 = new Triangle();
triangle2.calcPerimeter();
assertEquals(0.0, triangle2.getPerimeter(), 0.1);
triangle2.setSides(38, 39, 40);
assertEquals(0.0, triangle2.getPerimeter(), 0.1);
triangle2.calcPerimeter();
assertEquals(117.0, triangle2.getPerimeter(), 0.1);
}
#Test
public void testSetSidesAndTheArea()
{
Triangle triangle2 = new Triangle();
assertEquals(0.0, triangle2.getTheArea(), 0.1);
triangle2.setSides(12, 13, 14);
triangle2.calcArea();
assertEquals(-0.0, triangle1.getTheArea(), 0.1);
triangle2.calcPerimeter();
triangle2.calcArea();
assertEquals(72.30794, triangle2.getTheArea(), 0.00001);
}
}
And the error message said something was wrong with this line:
assertEquals(72.30794, triangle2.getTheArea(), 0.00001);
for some reason it causes it to fail. I do not know why, everything else works perfectly. Any help is appreciated.
Thanks
-Keelen
You forgot to take the square root of the expression you got for the area. And it seems that you are not using the right variable names:
public void calcArea( )
{
// ensure perimeter is up-to-date!
calcPerimeter();
double s = (perimeter / 2.0);
theArea = Math.sqrt(s * (s - sideA) * (s - sideB) * (s - sideC));
}
In the code
theArea=(s*(s-a)*(s-b)*(s-c));
should be
theArea=Math.sqrt((s*(s-a)*(s-b)*(s-c)));
This is because you have ill-code while calculating area of the triangle.
It should be as follows:
public void calcArea( )
{
double s;
s=(perimeter/2);
theArea=Math.sqrt((s*(s-a)*(s-b)*(s-c)));
}
Hope this helps.
Related
Can someone please help me correct my code ? It is created to calculate housing loan (UMI), the coding is correct but it doesn't show the output completely. After "Enter loan duration", it shows a box. Also is there another way to set the variables in class Variables ?
package assignment.pkg2;
import java.util.Scanner;//for Scanner
import java.text.DecimalFormat;//for using decimal format
class Variables{ //set the variables
private double p, r, n;
public void setVarP (double amount){
this.p = amount;
}
public void setVarR (double rate){
this.r = rate;
}
public void setVarN (int duration){
this.n = duration;
}
public double getVarP(){
return p;
}
public double getVarR(){
return r;
}
public double getVarN(){
return n;
}
}
class EMIcalc{ //the calculating part
private double monthlyPay, pow;
Variables var = new Variables();
Scanner scanner = new Scanner(System.in);
public void getPay(){
pow = Math.pow (1+(var.getVarR()/12), - var.getVarN());
monthlyPay = var.getVarP() * ( (var.getVarR()/12) / (1 - pow) );
DecimalFormat Dformat = new DecimalFormat("##.##");
System.out.println(Dformat.format(monthlyPay));
}
}
public class Assignment2 {
public static void main(String[] args) {
Variables var1 = new Variables();
EMIcalc calc = new EMIcalc();
Scanner scanner = new Scanner(System.in);
System.out.println("Enter your loan amount");
var1.setVarP(scanner.nextDouble());
System.out.println("Enter interest rate");
var1.setVarR(scanner.nextDouble());
System.out.println("Enter loan duration");
var1.setVarN(scanner.nextInt());
calc.getPay();
}
}
Your are setting the values into var1, but the calc looks at its internal var field, which is a different object. You could just replace var1.set... by calc.var.set....
Or you could merge your Variables and EMIcalc classes into one.
Are you looking for this?
import java.text.DecimalFormat;
import java.util.Scanner;
class Variables { //set the variables
private double p, r, n;
public double getVarP() {
return p;
}
public void setVarP(double amount) {
this.p = amount;
}
public double getVarR() {
return r;
}
public void setVarR(double rate) {
this.r = rate;
}
public double getVarN() {
return n;
}
public void setVarN(int duration) {
this.n = duration;
}
}
class EMIcalc { //the calculating part
Variables var = new Variables();
Scanner scanner = new Scanner(System.in);
private double monthlyPay, pow;
public void getPay() {
pow = Math.pow(1 + (var.getVarR() / 12), -var.getVarN());
monthlyPay = var.getVarP() * ((var.getVarR() / 12) / (1 - pow));
DecimalFormat Dformat = new DecimalFormat("##.##");
System.out.println(Dformat.format(monthlyPay));
}
}
public class Assignment2 {
public static void main(String[] args) {
EMIcalc calc = new EMIcalc();
Scanner scanner = new Scanner(System.in);
System.out.println("Enter your loan amount");
calc.var.setVarP(scanner.nextDouble());
System.out.println("Enter interest rate");
calc.var.setVarR(scanner.nextDouble());
System.out.println("Enter loan duration");
calc.var.setVarN(scanner.nextInt());
calc.getPay();
}
}
PS: Try your assignments by yourself.
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 am following along with the Java for Dummies book and I ran into a problem. I can't figure out why #Override isn't working. I'm sure it has to do with my code because I have gotten a polymorphic array to work with override before, but it was too simple for me to mimic.
import static java.lang.System.out;
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
public class DoPayrollTypeP {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
Scanner diskScanner = new Scanner(new File("EmpInfoNew.txt"));
Scanner kbdScanner = new Scanner (System.in);
for(int empNum = 1; empNum<=3; empNum++){
payOneFTEmployee (diskScanner);
}
for(int empNum = 4; empNum<=6; empNum++){
payOnePTEmployee(diskScanner, kbdScanner);
}
}
public static void payOneFTEmployee(Scanner diskScanner){
FullTimeEmployee ftemployee = new FullTimeEmployee();
ftemployee.setName(diskScanner.nextLine());
ftemployee.setJobTitle(diskScanner.nextLine());
ftemployee.setWeeklySalary(diskScanner.nextDouble());
ftemployee.setBenefitDeduction(diskScanner.nextDouble());
diskScanner.nextLine();
diskScanner.nextLine();
ftemployee.cutCheck(ftemployee.findPaymentAmount());
out.println();
}
public static void payOnePTEmployee(Scanner diskScanner, Scanner kbdScanner) {
PartTimeEmployee ptemployee = new PartTimeEmployee();
ptemployee.setName(diskScanner.nextLine());
ptemployee.setJobTitle(diskScanner.nextLine());
ptemployee.setHourlyRate(diskScanner.nextDouble());
diskScanner.nextLine();
diskScanner.nextLine(); //Reads the dashed line that
// separates two employees
out.print("Enter ");
out.print(ptemployee.getName());
out.print("'s hours worked this week: ");
int hours = kbdScanner.nextInt();
ptemployee.cutCheck(ptemployee.findPaymentAmount(hours));
out.println();
}
}
Next class:
import static java.lang.System.out;
public class Employee {
private String name;
private String jobTitle;
public void setName(String nameIn) {
name = nameIn;
}
public String getName() {
return name;
}
public void setJobTitle(String jobTitleIn) {
jobTitle = jobTitleIn;
}
public String getJobTitle() {
return jobTitle;
}
public void cutCheck(double amountPaid){
out.printf("Pay to the order of %s", name);
out.printf("(%s) ***$", jobTitle);
out.printf("%,.2f\n", amountPaid);
}
}
Next Class:
public class PartTimeEmployee extends Employee{
private double hourlyRate;
public void setHourlyRate(double hourlyRateIn) {
hourlyRate = hourlyRateIn;
}
public double getHourlyRate() {
return hourlyRate;
}
public double findPaymentAmount(int hours){
return hourlyRate * hours;
} //method that should be overriden
}
Class That should override:
public class PartTimeWithOver extends PartTimeEmployee{
#Override
public double findPaymentAmount(int hours) {
if(hours <= 40) {
return getHourlyRate() * hours;
} else {
return getHourlyRate() * 40 +
getHourlyRate() * 2 * (hours - 40);
}
}
}
EmpInfoNew.(txt) file
jo shmo
Ceo
5000.00
500.00
edd shmoe
Captain
5000.00
500.00
bob shmo
Honorary Exec
1000.00
200.00
Dave shmo
driver
7.25
edd blah
Cook
8.50
len shmo
Head of Kitchen
12.50
You have instantiated object as
PartTimeEmployee ptemployee = new PartTimeEmployee();
It is a base class of PartTimeWithOther so it cannot call overridden method in derived class.
Change to
PartTimeEmployee ptemployee = new PartTimeWithOver();
Here is an nice tutorial with diagrams that explains Polymorphism
You never use PartTimeWithOver, you only work with PartTimeEmployee.
So your overriden method is never called.
public class RightTriangle
{
private double leg_1;
private double leg_2;
public RightTriangle ()
{
leg_1 = 1;
leg_2 = 1;
}
public RightTriangle (double s1, double s2)
{
leg_1= s1 ;
leg_2= s2 ;
}
public double findArea ()
{
double area= ((leg_1+leg_2)/2);
return area;
}
public double findPerimeter ()
{
double s3Squared= Math.pow(leg_1,2) + Math.pow( leg_2,2);
double s3= Math.sqrt(s3Squared);
double perimeter=(leg_1 + leg_2 + s3);
return perimeter;
}
public void dilate (double factor)
{
}
RightTriangle t1 = new RightTriangle (3, 4);
t1.findArea();
}
For my Java program, the constructor should construct a triangle by default with two legs with a length of one. The other constructor allows you to choose the length of the triangles two legs.
I'm trying to test my program's methods by running the "findArea" method with my t1 triangle object, however when I try to run the program I get a "identifier expeced after the token error with my my t1.findArea() code highlighted. Please help me fix this error.
You need to create a main class to get your program started. Also, this line:
t1.findArea();
needs to be placed inside a method. I propose you change the last two lines of code to this:
public static void main(String[] args) {
RightTriangle t1 = new RightTriangle (3, 4);
t1.findArea();
}
You have to add a main method to make your class runnable.
public static void main(String args[]) {
RightTriangle t1 = new RightTriangle(3, 4);
System.out.println(t1.findArea());
}
So the full code will be
public class RightTriangle {
private double leg_1;
private double leg_2;
public RightTriangle() {
leg_1 = 1;
leg_2 = 1;
}
public RightTriangle(double s1, double s2) {
leg_1 = s1;
leg_2 = s2;
}
public double findArea() {
double area = ((leg_1 + leg_2) / 2);
return area;
}
public double findPerimeter() {
double s3Squared = Math.pow(leg_1, 2) + Math.pow(leg_2, 2);
double s3 = Math.sqrt(s3Squared);
double perimeter = (leg_1 + leg_2 + s3);
return perimeter;
}
public void dilate(double factor) {
}
public static void main(String args[]) {
RightTriangle t1 = new RightTriangle(3, 4);
System.out.println(t1.findArea());
}
}
RightTriangle t1 = new RightTriangle (3, 4);
t1.findArea();
That needs to be in a method called through main
public static void main(String[] args)
{
RightTriangle t1 = new RightTriangle (3, 4);
t1.findArea();
}
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) {
...
}
}