I'm doing a problem where the area of a triangle is returned (if valid). However, I want to return a message (i.e. 'triangle is not valid) if the triangle is invalid.
I'm not sure how to go about to doing this as my method (called area) will only let me return doubles. Possible to return a string in an else within my area method?
public class MyTriangle { public static void main(String[] args) { //triangle is valid if the sum of any two sides is bigger than the third System.out.println(isValid(3, 4, 5)); System.out.println(area(543, 4, 5));
I've been having trouble with this code for about a week and I've finally got it down to one error. Here is the code:
import java.util.Scanner; public class Triangle { public static void main (String[] args) { Scanner Console = new Scanner(System.in); System.out.print("Please enter the three lengths of your Triangle: "); double a = console.nextDouble();
[Code] ....
And here is the error:
Triangle.java:30: error: class, interface, or enum expected } // End class ^ 1 error
My homework assignment is: Using a do-while statement, write a Java program that continuously requests a grade to be entered. If the grade is less than 0 or greater than 100, your program should display an appropriate message informing the user that an invalid grade has been entered; a valid grade should be added to a total. When a grade of 999 is entered, the program should exit the repetition loop and compute and display the average of the valid grades entered. Run the program on your computer and verify the program using appropriate test data.
When I run it, I don't get the correct average. I know that i'm supposed to enter 999 to exit the loop and to display the average, but 999 is being added with the loop. I'm not sure how to make it not do that.
//stephanie import java.util.Scanner; public class gradeAverage { public static void main(String[] args)
package areatest; import javax.swing.JOptionPane; public class AreaTest {
public static double areaTriangle (double length, double width){ //How to calculate the area of a triangle return .5f * length * width;
[Code] .....
When I try to get the area of a rectangle it gives me 9 no matter what input I give it. When I try to get the area of a triangle it gives me .5 no matter what input I give it. Same with the circle but it always gives me 12.56370...
We have triangle made of blocks. The topmost row has 1 block, the next row down has 2 blocks, the next row has 3 blocks, and so on. Compute recursively (no loops or multiplication) the total number of blocks in such a triangle with the given number of rows.
import javax.swing.JOptionPane; import java.util.*; import java.text.DecimalFormat; import java.util.StringTokenizer; public class ShelbyHarms_3_03 { public static void main (String [] args) { double a, b, c; //Input sides of triangle double x; //Perimeter of triangle double area; //Area of triangle
[Code] .....
Here are the errors:
ShelbyHarms_3_03.java:39: error: variable x might not have been initialized JOptionPane.showMessageDialog(null, formatter.format(x)); ^ ShelbyHarms_3_03.java:42: error: variable area might not have been initialized JOptionPane.showMessageDialog(null, formatter.format(area)); ^ 2 errors
I started with finding the area of a triangle, but now I'm trying to ask a user what kind of shape they want the area for, then ask questions to get the area. I can't figure out how to take the shape a person types to go to a certain case. It also says shape hasn't been initialized. I don't know how to do that.
import java.util.Scanner; public class TriangleArea { static Scanner sc = new Scanner(System.in); public static void main(String[] args){ char shape; String text = "Do you want to find the area of a triangle, square, rectangle, or trapezoid?"; System.out.print("Text"); switch(shape){
Basically its a program where a user is prompted to enter the length of all three sides of a triangle and the program calculates the area by herons formula and can tell if the triangle is equilateral or Pythagorean. I am having trouble entering a formula to where all three enter sides cant possibly be a triangle. Here is my Program. Where the '?' is stated.
import java.util.Scanner; public class Triangle { public static void main(String[] args){ double a; double b; double c; double s; double x; double area;
Allow the user to specify the number of terms (5 terms are shown) to use in the computation. Each time around the loop only one extra term should be added to the estimate for PI.
Alter your solution from part one so that the user is allowed to specify the precision required between 1 and 8 digits (i.e. the number of digits which are correct; e.g. to 5 digits PI is 3.14159), rather than the number of terms. The condition on the loop should be altered so that it continues until the required precision is obtained. Note that you need only submit this second version of the program (assuming you have it working).
My output for part 1 is :
Enter number of wanted terms for value of PI 100 PI = 3.1666666666666665 Number of Term = 2 PI = 3.1333333333333333 Number of Term = 3 PI = 3.145238095238095 Number of Term = 4 PI = 3.1396825396825396 Number of Term = 5 PI = 3.1427128427128426 Number of Term = 6 PI = 3.1408813408813407 Number of Term = 7 PI = 3.142071817071817 Number of Term = 8 PI = 3.1412548236077646 Number of Term = 9 PI = 3.141839618929402 Number of Term = 10 ................ and so on
I know you have to compare value of current Pi to the previous one but I'm stuck.
Four experiments are performed, each consisting of six tests. The number of test results for each experiment is entered by the user. Write a Java program using a nested loop to compute and display the average of the test results for each experiment.
You can run the program by entering the following test results:
I am new to java and do not understand how to use filters very well. I need to write a code that determines if an integer is not divisible by some value. A filter also contains an upstream filter so that this filter can be part of a chain of filters.
I want to do an app that capture a selected area of a screen and save it. I did a few research and i did the code down below.
My questions are:
1 - How can i open a pdf file in this app ? (i tried use a method but it didnt work. I dont know exactly where to put it on the code)
2 - How can i save the selected area in a new file ? (a image file : JPEG, JPG,png)
3 - [the complex part] right now, the code only "save" one selected area each time. I want to capture a lot of parts of screen and save this in the same image file. one beside the other. How can i do this ?
Write a Java program that calculates the area and volume of a cube, sphere, cylinder, and regular tetrahedron. Your program should obtain the necessary input for each of the objects from the console and output the input, area, and volume of the object. Use appropriate messages to guide the user of your program.
Here what i did, i am not sure if this is what the assignment want, but this is the best method i could come up with
//Khang Le import java.util.Scanner; public class InputAreaVolume { public static void main(String[] args) {
This is what I have so far, but I don't know how to continue:
import java.util.Scanner; public class ArithmeticComputation{ public static void main(String[] args) { Scanner stdin = new Scanner(System.in); int num1 = stdin.nextInt();
[Code] ....
I'm looking for a method that allows me to assign char into actual operators, so that I can loop through all the computation.
String must be 6 characters, first 3 must be letters, last 3 must be digits..Here is my code:
import java.util.Scanner; public class ValidTicket { public static void main (String[] args){ Scanner in = new Scanner (System.in); String id; int c=' ',i; int letterCount=0, digitCount=0; System.out.printf("Enter ticked id
[code]....
However when I run my program, it does not work:After I enter the string nothing happens.
I am required to make a console calculator that takes the values as strings and turns them into doubles. The user needs to be able to enter something like 3+4+5*9/13 on one line and the application give the answer. As of this point I am currently on at the last section as making the user be able to enter the expression and present the answer. I have added an array list containing + - / * and the same with the numbers 1-9...I now need to actually get the information to all come together. I'm not sure how to do that. I *think* I have all the parts for it to work.
public static void main(String[] args) { String input = ""; // initalize the string boolean isOn = true; // used for the while loop...when false, the // program will exit. final String exitCommand = "Exit"; // exit command
This is what I need to do: Write a method called scientific that accepts two real numbers as parameters for a base and an exponent and computes the base times 10 to the exponent, as seen in scientific notation. For example, the call of scientific(6.23, 5.0) would return 623000.0 and the call of scientific(1.9, -2.0) would return 0.019.
This is the method I have written:
public double scientific(double num1, double num2); { double base = num1; double exp = num2; double scientific = base * Math.pow(10, exp); System.out.println(scientific); return scientific; }
These are the following error messages I received when trying to compile the code:
Line 4 missing method body, or declare abstract missing method body, or declare abstract public double scientific(double num1, double num2);
My verify method also always returns false. So I'm given three classes to begin with. Calculator, Expression, and InfixExpression and they are listed below.
The goal is to create a class called PostfixExpression that extends Expression and can read and calculate postfix expressions.
My evaluate() method works for most calculations but when it needs to return a negative value it just returns the positive equivalent.
Also, my verify method always returns false and I can't pinpoint why.
Here's my current code. Some things are commented out for debugging purposes.
import java.util.Scanner; /** * Simple calculator that reads infix expressions and evaluates them. */ public class Calculator { public static void main(String[] args) { Scanner input = new Scanner(System.in);
<import javax.swing.JOptionPane; public class TempCalc {
public static void main(String[] args) {
//Confirming which conversion to compute. String FarOrCel = (JOptionPane.showInputDialog("Would you like to calculate a Farenheit value or a Celsius value? (Enter Farenheit or Celsius)");
[Code] ....
So I've been trying to come up with a Farenheit to Celsius converter and back. However, whenever I run the program in JCreator it never recognizes the string "Farenheit". Even if I put that exact thing (I copy/pasted from the if (FarOrCel == "Farenheit")) it keeps asking me for a Celsius value.
[quote name='INNOCENTREAPER##!' date='28 September 2014 - 07:40 PM' timestamp='1411958437' post='2057072']
<import javax.swing.JOptionPane; public class TempCalc { public static void main(String[] args) {
//Confirming which conversion to compute. String FarOrCel = JOptionPane.showInputDialog("Would you like to calculate a Farenheit value or a Celsius value? (Enter Farenheit or Celsius)";
Basically I'm trying to compute the standard deviation for a set of numbers and I'm using previously know data to compute this. The data I'm working with is here (note all of this is fake as its for an assignment):
Next this is the code that I know is producing the error:
/** * Pass in original data from files, and pass in computed grades as arraylist. this returns all of the standard deviations to be printed */ public static ArrayList calcStandDev(String data1[], ArrayList<Double> grades, ArrayList<Integer> averages) { double standDev = 0; int j = 0; //gets rid of first element in string array, makes processing MUCH easier String[] data = Arrays.copyOfRange(data1, 1, data1.length);
[Code] ....
Look for the line with all the ***'s, it is producing the "java.lang.ClassCastException: java.lang.Double cannot be cast to java.lang.Integer".
The reason I dont undersand why its producing this error is because temp.get(i) should be a integer - averages.get(i) (is type double), I do believe automatic up casting is present here making them both a double then I multiply it by itself producing a double? I dont understand where the casing is occurring. And just incase you want the formula I'm using for standard deviation here it is:
I have some problems about performing regular expression. In the following code, the output as a result is being "valid last name". But in my opinion, it should be "invalid last name". Because "Doe" has not any space, apostroph or any hypen. Look at the regular expression statement: "[a-zA-Z]+([ '-][a-zA-Z]+)*"
package deneme; public class Deneme { public static void main(String[] args) { String lastName = "Mc-Something"; // Mc-Something if(!lastName.matches("[a-zA-Z]+([ '-][a-zA-Z]+)*")) System.out.println("Invalid last name"); else System.out.println("Valid last name"); } }
I know that the asterisk qualifier means character(s) in any number.(Is this wrong?) So for "Mc-Something", 'M' corresponds to [a-zA-Z], 'c' corresponds to +, - corresponds to [ '-], 'S' corresponds to [a-zA-Z], "o" corresponds to +, and finally "mething" corresponds to *, doesn't they? But for "Doe", there is no any space, apostroph or hypen.