"Cannot find symbol error" in my Triangle class - java

I can't figure out why I'm getting the cannot find symbol errors for the num and triangle type variables.
This is the class:
public class Triangle
{
private int s1;
private int s2;
private int s3;
public Triangle (int s1, int s2, int s3)
{
s1= num1;
s2= num2;
s3= num3;
}
public String toString()
{
return (s1 + " " + s2 + " " + s3);
}
public boolean is_equilateral(){
if(s1==s2 && s2==s3 && s3==s1)
{
return Triangle.is_equilateral;
}
else
{
return false;
}
}
public boolean is_isosceles(){
if((s1==s2)||(s2==s3)||(s3==s1))
{
return Triangle.is_isosceles;
}
else
{
return false;
}
}
public boolean is_scalene(){
if(s1!=s2 && s2!=s3 && s3!=s1)
{
return Triangle.is_scalene;
}
else
{
return false;
}
}
}
and this is the program:
import java.util.Scanner;
public class Assignment5 {
//===========================================================
// Create and determine properties of various triangles.
//===========================================================
public static void main (String[] args) {
Scanner console = new Scanner(System.in);
int num1;
int num2;
int num3;
String another;
do
{
System.out.println("Enter the sides of the triangle: ");
num1 = console.nextInt();
num2 = console.nextInt();
num3 = console.nextInt();
Triangle myTriangle = new Triangle (num1, num2, num3);
System.out.println(myTriangle.toString() + " triangle:");
//check the isosceles
if (myTriangle.is_isosceles())
System.out.println("\tIt is isosceles");
else
System.out.println("\tIt is not isosceles");
//check the equilateral
if (myTriangle.is_equilateral())
System.out.println("\tIt is equilateral");
else
System.out.println("\tIt is not a equilateral");
//check the scalene
if (myTriangle.is_scalene())
System.out.println("\tIt is scalene");
else
System.out.println("\tIt is not scalene");
System.out.println();
System.out.print("Check another Triangle (y/n)? ");
another = console.next();
} while (another.equalsIgnoreCase("y"));
} // method main
} // class Assignment5
I'm fairly new to Java so sorry if this is an obvious problem.

private int s1; // these belong to the class
private int s2;
private int s3;
public Triangle (int s1, int s2, int s3) // these belong to the constructor
{
s1= num1; // num1 isn't declared anywhere
s2= num2;
s3= num3;
}
Both your class variables and the arguments to your constructor are named s1 - s3.
You need to either change the arguments to num1 - num3, or change the assignment statements to use the this keyword to refer to the class variables. Here's the first method:
private int s1; // these belong to the class
private int s2;
private int s3;
public Triangle (int num1, int num2, int num3) // these belong to the constructor
{
s1 = num1;
s2 = num2;
s3 = num3;
}
Providing Constructors for Your Classes

Related

How to give 2 different data types from a method

I want to do a simple beginner project using methods, if statements, and user input. I am having an issue though with the calc() method. How can I return two different data types in java, and if I cannot, how could I do it, still by using more than the main method?
import java.util.Scanner; //allow user input
public class fourFunctionCalculator{
public static void main(String[] args) {
Scanner keyboardInput = new Scanner(System.in);
System.out.print("Enter your first number:"); //get first number
double num1 = keyboardInput.nextDouble();
System.out.print("Enter your operator: "); // get operator
String name = keyboardInput.next(); //grabs everything user types until a space
System.out.print("Enter your second number: "); //get second number
double num2 = keyboardInput.nextDouble();
System.out.println(calc(num1,op,num2));
}
//troublesome part is here
public static double calc(double num1, String op, double num2){
if (op == "+") {
return (num1 + num2);
}
else if (op == "-") {
return (num1 - num2);
}
else if (op == "*") {
return (num1 * num2);
}
else if (op == "/") {
return (num1 / num2);
}
else {
return ("INVALID OPERATOR");
}
}
}
you could generate a custom Exception, also you need to use the method .equals() inside the if validations, otherwise it is not going to work.
fourFunctionCalculator.java
import java.util.Scanner; //allow user input
public class fourFunctionCalculator{
public static void main(String[] args) {
Scanner keyboardInput = new Scanner(System.in);
System.out.print("Enter your first number:"); //get first number
double num1 = keyboardInput.nextDouble();
System.out.print("Enter your operator: "); // get operator
String name = keyboardInput.next(); //grabs everything user types until a space
System.out.print("Enter your second number: "); //get second number
double num2 = keyboardInput.nextDouble();
try {
System.out.println(calc(num1,name,num2));
} catch (InvalidOperatorException e) {
System.out.println(e);
}
}
public static double calc(double num1, String op, double num2){
if (op.equals("+")) {
return (num1 + num2);
}
else if (op.equals("-")) {
return (num1 - num2);
}
else if (op.equals("*")) {
return (num1 * num2);
}
else if (op.equals("/")) {enter code here
return (num1 / num2);
}
throw new InvalidOperatorException("INVALID OPERATOR : " + op);
}
}
InvalidOperatorException.java
public class InvalidOperatorException
extends RuntimeException {
private static final long serialVersionUID = 1L;
public InvalidOperatorException(String errorMessage) {
super(errorMessage);
}
}
I recommend returning an OptionalDouble object as placeholder of something valid or not...
Ex #1: return OptionalDouble.of(num1 + num2); // first if outcome
Ex #2: return OptionalDouble.empty(); // for the last else
Then your main(...) method needs to be something like...
System.out.println(calc(num1,op,num2).orElseThrow(() -> new IllegalArgumentException("INVALID OPERATOR")));
I suggest returning always String.
public static String calc(double num1, String op, double num2){
if (op == "+") {
return String.valueOf(num1 + num2);
}
else if (op == "-") {
return String.valueOf(num1 - num2);
}
else if (op == "*") {
return String.valueOf(num1 * num2);
}
else if (op == "/") {
return String.valueOf(num1 / num2);
}
else {
return ("INVALID OPERATOR");
}
}

Double not being passed down in from method to method

I designed a small program where I have three exam grades and I use the Grades class to compute the average of the three. Also, I prompt for the exam number (1,2, or 3) and it should return it. However, I keep getting 0.0 as the output for both the average exam score and chosen exam score.
package GradesClass;
import java.util.Scanner;
public class GradesDriver {
public static void main(String[] args) {
Grades school = new Grades(90.9,87.9,99.9);
Scanner in = new Scanner(System.in);
System.out.println("Enter desired test number: ");
int testnumber = in.nextInt();
System.out.println(school);
System.out.println("Exam score: " + school.getGrades(testnumber));
}
}
package GradesClass;
public class Grades {
private double num1, num2, num3;
private int testnumber;
private double average;
public Grades(double num1, double num2, double num3) {
num1 = 0;
num2 = 0;
num3 = 0;
}
public void setGrades(double scorenumber, int testnumber) {
if (testnumber == 1) {
num1 = scorenumber;
} else if (testnumber == 2) {
num2 = scorenumber;
} else {
num3 = scorenumber;
}
}
public double getGrades(int testnumber) {
if (testnumber == 1) {
return(num1);
} else if (testnumber == 2) {
return(num2);
} else {
return(num3);
}
}
public double average(double num1, double num2, double num3) {
average = ((num1+num2+num3)/3.0);
return(average);
}
public String toString() {
return("Average: " + average);
}
}
In your constructor for Grades you are setting the member variables to zero instead of the values supplied in the parameters. Change the constructor to
public Grades(double num1, double num2, double num3) {
this.num1 = num1;
this.num2 = num2;
this.num3 = num3;
}

Are my loops preventing me from getting the expected output?

My goal is to determine, for a pair of integers, whether the second integer is a multiple of the first. My code is as follows:
import java.util.Scanner;
public class Multiples {
public static void main(String [] args) {
boolean run = true;
while(run) {
Scanner input = new Scanner(System.in);
System.out.print("Enter one number:");
int num1 = input.nextInt();
System.out.print("Enter a second number:");
int num2 = input.nextInt();
boolean result = isMultiple(num1, num2);
if(result) {
System.out.println(num2 + " is a multiple of " + num1);
} else {
System.out.println(num2 + " is not a multiple of " + num1);
}
System.out.print("Do you want to enter another pair(y/n)?");
run = YesOrNo(input.next());
}
}
public static boolean YesOrNo(String a) {
if(a.equals("y"))
return true;
else
return false;
}
public static boolean isMultiple (int x , int y) {
if(y % x == 0)
return true;
else
return false;
}
}
This is my output:
This is the expected output:
The expected output is confusing me.

Scanner cannot be used when declared, sc cannot be resolved

The only problem I'm having with my program is the scanner, I've used it many times but in this program it won't run right. The error is in the public static double getCandlecost() and the public static int getShippingType() methods. under the int shippingType = sc.nextInt(); and the double candleCost = sc.nextDouble(); both say "sc cannot be resolved" and in my main class I did declare it.
import java.util.Scanner;
import java.text.DecimalFormat;
public class Candel {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double candleCost, shippingCost;
int shippingType;
candleCost = getCandlecost();
shippingType = getShippingType();
shippingCost = getShippingCost(candleCost, shippingType);
output(candleCost, shippingCost);
}
public static double getCandlecost()
{
boolean done = false;
do{
try
{
System.out.print("Enter the cost of the candle order ");
double candleCost = sc.nextDouble();
done = true;
return candleCost;
} catch (InputMismatchException e)
{
System.out.println("Error, Enter a dollar amount greater than 0");
}
} while (!done);
return 0;
}
public static int getShippingType()
{
System.out.println("Enter the type of shipping: ");
System.out.println("1> Priority <overnight>");
System.out.println("2> Express <2 business days>");
System.out.println("3> Standard <3 to 7 business days>");
System.out.println("Enter type number: ");
int shippingType = sc.nextInt();
if(shippingType == 1){}
else if(shippingType == 2){}
else if(shippingType == 3){}
return shippingType;
}
public static double getShippingCost(double candleCost, int shippingType)
{
switch(shippingType)
{
case 1:
candleCost = 16.95 + candleCost;
break;
case 2:
candleCost = 13.95 + candleCost;
break;
case 3:
if (candleCost > 100.00){
candleCost = candleCost;
}
else{
candleCost = 7.95 + candleCost;
}
break;
}
return candleCost;
}
public static void output(double candleCost, double shippingCost)
{
DecimalFormat twoDigits = new DecimalFormat("$#,000.00");
System.out.println("The candle cost of " + twoDigits.format(candleCost) + " with shipping costs of "
+ shippingCost + " equals " + twoDigits.format(candleCost + shippingCost));
}
}
Your scanner is out of scope. Since you have declared it in the main() method, only that method can access it. Your scanner needs to be static as well. Write it like this instead:
public class Candel {
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
...
}
...
}

String won't return

The method getResponseToCorrectAnswer and getResponseToIncorrectAnswer won't return any strings, but a print will work, so it's getting to the method. Does anyone know how to get these two methods to correctly return a String?
import java.util.Scanner;
public class CAI {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int answer;
for(int i = 1; i < 11; i++){
int num1 = generateRandomSingleDigitNumber();
int num2 = generateRandomSingleDigitNumber();
generateMultiplicationQuestion(num1, num2);
answer = input.nextInt();
validateMultiplicationAnswer(num1, num2, answer);
}//end for
}//end main
public static void generateMultiplicationQuestion(int num1, int num2){
System.out.println("What is the product of " + num1 + " x " + num2 + "?");
}//end generateMultiplicationQuestion
public static int generateRandomSingleDigitNumber(){
return (int)(Math.random()*10);
}//end generateRandomSingleDigitNumber
public static String getResponseToCorrectAnswer(){
//return ("Correct");
int num;
num = (int)(Math.random()*4);
if(num == 3){
return("Very good");
}else if(num == 2){
return("Excellent!");
}else if(num == 1){
return("Nice work!");
}else{
return("Keep up the good work!");
}//end if
}//end getResponseToCorrectAnswer
public static String getResponseToIncorrectAnswer(){
int num;
num = (int)(Math.random()*4);
if(num == 3){
return("No. Please try again.");
}else if(num == 2){
return("Wrong. Try once more");
}else if(num == 1){
return("Don't give up. Keep trying.");
}else{
return("Incorrect. Please answer again.");
}//end if
}//end getResponseToIncorrectAnswer
public static boolean validateMultiplicationAnswer(int num1, int num2, int answer) {
if(answer == num1 * num2){
getResponseToCorrectAnswer();
return true;
}else{
getResponseToIncorrectAnswer();
return false;
}//end if
}//end validateMultiplicationAnswer
}//end class
the method getResponseToCorrectAnswer and getResponseToIncorrectAnswer won't return any strings
Sure it will, but you're not assigning the string to anything so you don't see it.
Try this
String response = getResponseToCorrectAnswer();
System.out.println(response);
and you will see that response holds the return value.

Categories