I creating a program where it takes a base unit in the metric system. (Say grams.) And then when you select a prefix changes it to the equivalent amount. (Such as 1000 grams when you select Kilo would change it to 1 Kilogram.)
Problem is when I run the code it'll always end up as zero, which makes me think I'm manipulating the BigIntegers wrong. (I'm using VERY large numbers due to some prefixes being very small or very large beyond the usual long number.)
Here is the code:
import java.util.Scanner;
import java.math.BigInteger;
public class BaseMetricUnits {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Hello, World!");
float meter = 1;
float kilogram = 1;
float second = 1;
float ampere = 1;
float kelvin = 1;
float mole = 1;
float candela = 1;
PrefixConverter();
}
public static void PrefixConverter()
{
BigInteger Yocto = BigInteger.valueOf((long) 0.00000000000000000000000);
BigInteger Zepto = BigInteger.valueOf((long) 0.000000000000000000001);
BigInteger Atto = BigInteger.valueOf((long) 0.000000000000000001);
BigInteger Femto = BigInteger.valueOf((long) 0.000000000000001);
BigInteger Pico = BigInteger.valueOf((long)0.000000000001);
BigInteger Nano = BigInteger.valueOf((long)0.000000001);
BigInteger Micro = BigInteger.valueOf((long)0.000001);
BigInteger Milli = BigInteger.valueOf((long)0.001);
BigInteger Centi = BigInteger.valueOf((long)0.01);
BigInteger Deci = BigInteger.valueOf((long)0.1);
BigInteger Deca = BigInteger.valueOf((long)10);
BigInteger Hecto = BigInteger.valueOf((long)100);
BigInteger Kilo = BigInteger.valueOf((long)1000);
BigInteger Mega = BigInteger.valueOf((long)1000000);
BigInteger Giga = BigInteger.valueOf((long)1000000000);
BigInteger Tera = new BigInteger("1000000000000");
BigInteger Peta = new BigInteger("1000000000000000");
BigInteger Exa = new BigInteger("1000000000000000000");
BigInteger Zetta = new BigInteger("1000000000000000000000");
BigInteger Yotta = new BigInteger("1000000000000000000000000");
long Amount;
double Prefix;
String Units = "";
BigInteger translatedResult;
BigInteger Result;
Scanner inputDevice = new Scanner(System.in);
System.out.print("Please enter the type of unit to be used. (meters, grams, etc.) >> ");
Units = inputDevice.next();
System.out.print("Please enter an amount to be translated >> ");
Amount = inputDevice.nextLong();
System.out.print("Please choose one of the following Prefixes to translate to. ");
System.out.print(" 1 - Yocto ");
System.out.print(" 2 - Zepto ");
System.out.print(" 3 - Atto ");
System.out.print(" 4 - Femto ");
System.out.print(" 5 - Pico ");
System.out.print(" 6 - Nano ");
System.out.print(" 7 - Micro ");
System.out.print(" 8 - Milli ");
System.out.print(" 9 - Centi ");
System.out.print(" 10 - Deci ");
System.out.print(" 11 - Deca ");
System.out.print(" 12 - Hecto ");
System.out.print(" 13 - Kilo ");
System.out.print(" 14 - Mega ");
System.out.print(" 15 - Giga ");
System.out.print(" 16 - Tera ");
System.out.print(" 17 - Peta ");
System.out.print(" 18 - Exa ");
System.out.print(" 19 - Zetta ");
System.out.print(" 20 - Yotta ") ;
Prefix = inputDevice.nextDouble();
if(Prefix == 1)
{
Result = Yocto.multiply(BigInteger.valueOf(Amount));
translatedResult = Yocto.divide(BigInteger.valueOf(Amount));
System.out.println("You have " + Result + " " + Units + " which translates to " + translatedResult + "Yocto" + Units + ".");
}
if(Prefix == 2)
{
Result = Zepto.multiply(BigInteger.valueOf(Amount));
translatedResult = Zepto.divide(BigInteger.valueOf(Amount));
System.out.println("You have " + Result + " " + Units + " which translates to " + translatedResult + "Zepto" + Units + ".");
}
if(Prefix == 3)
{
Result = Atto.multiply(BigInteger.valueOf(Amount));
translatedResult = Atto.divide(BigInteger.valueOf(Amount));
System.out.println("You have " + Result + " " + Units + " which translates to " + translatedResult + "Atto" + Units + ".");
}
if(Prefix == 4)
{
Result = Femto.multiply(BigInteger.valueOf(Amount));
translatedResult = Femto.divide(BigInteger.valueOf(Amount));
System.out.println("You have " + Result + " " + Units + " which translates to " + translatedResult + "Femto" + Units + ".");
}
if(Prefix == 5)
{
Result = Pico.multiply(BigInteger.valueOf(Amount));
translatedResult = Pico.divide(BigInteger.valueOf(Amount));
System.out.println("You have " + Result + " " + Units + " which translates to " + translatedResult + "Pico" + Units + ".");
}
if(Prefix == 6)
{
Result = Nano.multiply(BigInteger.valueOf(Amount));
translatedResult = Nano.divide(BigInteger.valueOf(Amount));
System.out.println("You have " + Result + " " + Units + " which translates to " + translatedResult + "Nano" + Units + ".");
}
if(Prefix == 7)
{
Result = Micro.multiply(BigInteger.valueOf(Amount));
translatedResult = Micro.divide(BigInteger.valueOf(Amount));
System.out.println("You have " + Result + " " + Units + " which translates to " + translatedResult + "Micro" + Units + ".");
}
if(Prefix == 8)
{
Result = Milli.multiply(BigInteger.valueOf(Amount));
translatedResult = Milli.divide(BigInteger.valueOf(Amount));
System.out.println("You have " + Result + " " + Units + " which translates to " + translatedResult + "Milli" + Units + ".");
}
if(Prefix == 9)
{
Result = Centi.multiply(BigInteger.valueOf(Amount));
translatedResult = Centi.divide(BigInteger.valueOf(Amount));
System.out.println("You have " + Result + " " + Units + " which translates to " + translatedResult + "Centi" + Units + ".");
}
if(Prefix == 10)
{
Result = Deci.multiply(BigInteger.valueOf(Amount));
translatedResult = Deci.divide(BigInteger.valueOf(Amount));
System.out.println("You have " + Result + " " + Units + " which translates to " + translatedResult + "Deci" + Units + ".");
}
if(Prefix == 11)
{
Result = Deca.multiply(BigInteger.valueOf(Amount));
translatedResult = Deca.divide(BigInteger.valueOf(Amount));
System.out.println("You have " + Result + " " + Units + " which translates to " + translatedResult + "Deca" + Units + ".");
}
if(Prefix == 12)
{
Result = Hecto.multiply(BigInteger.valueOf(Amount));
translatedResult = Hecto.divide(BigInteger.valueOf(Amount));
System.out.println("You have " + Result + " " + Units + " which translates to " + translatedResult + "Hecto" + Units + ".");
}
if(Prefix == 13)
{
Result = Kilo.multiply(BigInteger.valueOf(Amount));
translatedResult = Kilo.divide(BigInteger.valueOf(Amount));
System.out.println("You have " + Result + " " + Units + " which translates to " + translatedResult + "Kilo" + Units + ".");
}
if(Prefix == 14)
{
Result = Mega.multiply(BigInteger.valueOf(Amount));
translatedResult = Mega.divide(BigInteger.valueOf(Amount));
System.out.println("You have " + Result + " " + Units + " which translates to " + translatedResult + "Mega" + Units + ".");
}
if(Prefix == 15)
{
Result = Giga.multiply(BigInteger.valueOf(Amount));
translatedResult = Giga.divide(BigInteger.valueOf(Amount));
System.out.println("You have " + Result + " " + Units + " which translates to " + translatedResult + "Giga" + Units + ".");
}
if(Prefix == 16)
{
Result = Tera.multiply(BigInteger.valueOf(Amount));
translatedResult = Tera.divide(BigInteger.valueOf(Amount));
System.out.println("You have " + Result + " " + Units + " which translates to " + translatedResult + "Tera" + Units + ".");
}
if(Prefix == 17)
{
Result = Peta.multiply(BigInteger.valueOf(Amount));
translatedResult = Peta.divide(BigInteger.valueOf(Amount));
System.out.println("You have " + Result + " " + Units + " which translates to " + translatedResult + "Peta" + Units + ".");
}
if(Prefix == 18)
{
Result = Exa.multiply(BigInteger.valueOf(Amount));
translatedResult = Exa.divide(BigInteger.valueOf(Amount));
System.out.println("You have " + Result + " " + Units + " which translates to " + translatedResult + "Exa" + Units + ".");
}
if(Prefix == 19)
{
Result = Zetta.multiply(BigInteger.valueOf(Amount));
translatedResult = Zetta.divide(BigInteger.valueOf(Amount));
System.out.println("You have " + Result + " " + Units + " which translates to " + translatedResult + "Zetta" + Units + ".");
}
if(Prefix == 20)
{
Result = Yotta.multiply(BigInteger.valueOf(Amount));
translatedResult = Yotta.divide(BigInteger.valueOf(Amount));
System.out.println("You have " + Result + " " + Units + " which translates to " + translatedResult + "Yotta" + Units + ".");
}
else
{
System.out.println("Not a valid input.");
}
}
}
Thanks for your help.
BigInteger can only store integers, and so is not a suitable data type for this application. I strongly recommend replacing it with BigDecimal.
You should also use string representations of the fractions to initialize:
BigDecimal Zepto = new BigDecimal("0.000000000000000000001");
BigDecimal Atto = new BigDecimal("0.000000000000000001");
BigDecimal Femto = new BigDecimal("0.000000000000001");
BigDecimal Pico = new BigDecimal("0.000000000001");
BigDecimal Nano = new BigDecimal("0.000000001");
Comments on this answer and on the question indicate a concern with using BigDecimal to store the larger numbers. This program illustrates the fact that it is not an issue:
import java.math.BigDecimal;
public class Test {
public static void main(String[] args) {
BigDecimal googol = new BigDecimal("1e100");
System.out.println(googol);
System.out.println(googol.add(BigDecimal.ONE));
}
}
Output:
1E+100
10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
Related
I have an issue with it looping the same output even though I have entered the correct input to end the loop. This is the output that kept repeating Invalid input. Please enter again! Would you like to enter another student's mark? [Y/N]. I have looked at the code for possible error but I couldn't figure out where had the code gone wrong for an error to occur. Below is the full code.
import java.util.Scanner;
public class tutorial {
#SuppressWarnings("empty-statement")
public static void main (String args[]){
Scanner input = new Scanner(System.in);
String id, inpTutorial, inpAssignment, inpTest, inpProject, inpFinalExam, reenter = "", enter = "", reenterAgain = "", enterAgain = "";
int A = 80, B = 65, C = 50, D = 40, F = 0, bPlus = B + 5, cPlus = C + 5;
Double tutorial = 0.0, assignment = 0.0, test = 0.0, project = 0.0, finalExam = 0.0, tutorialMarks = 0.0, assignmentMarks =0.0, testMarks = 0.0, projectMarks = 0.0, finalExamMarks = 0.0;
double totalMarks = 0.0;
do {
do {
System.out.println("Please enter ID");
id = input.next();
if(id.isEmpty()) {
System.out.println("ID should not be empty!\n");
}
else {
do {
System.out.println("Please enter tutorial marks");
inpTutorial = input.next();
tutorial = Double.parseDouble(inpTutorial);
if (tutorial < 0) {
System.out.println("Tutorial marks cannot be a negative!\n");
}
else if (tutorial > 100) {
System.out.println("Tutorial marks can only be 100%\n");
}
else if (tutorial == null) {
System.out.println("Tutorial marks must have a value!");
}
else {
do {
System.out.println("Please enter assignment marks");
inpAssignment = input.next();
assignment = Double.parseDouble(inpAssignment);
if (assignment < 0) {
System.out.println("Assignment marks cannot be a negative!\n");
}
else if (assignment > 100) {
System.out.println("Assignment marks can only be 100%\n");
}
else if (assignment == null) {
System.out.println("Assignment marks must have a value!");
}
else {
do {
System.out.println("Please enter test marks");
inpTest = input.next();
test = Double.parseDouble(inpTest);
if (test < 0) {
System.out.println("Test marks cannot be a negative!\n");
}
else if (test > 100) {
System.out.println("Test marks can only be 100%\n");
}
else if (test == null) {
System.out.println("Test marks must have a value!");
}
else {
do {
System.out.println("Please enter project marks");
inpProject = input.next();
project = Double.parseDouble(inpProject);
if (project < 0) {
System.out.println("Project marks cannot be a negative!\n");
}
else if (project > 100) {
System.out.println("Project marks can only be 100%\n");
}
else if (project == null) {
System.out.println("Project marks must have a value!");
}
else {
do {
System.out.println("Please enter final examination marks");
inpFinalExam = input.next();
finalExam = Double.parseDouble(inpFinalExam);
if (finalExam < 0) {
System.out.println("Final examination marks cannot be a negative!\n");
}
else if (finalExam > 100) {
System.out.println("Final examination marks can only be 100%\n");
}
else if (finalExam == null) {
System.out.println("Final examination marks must have a value!");
}
else {
tutorialMarks = (tutorial/100)*10;
assignmentMarks = (assignment/100)*10;
testMarks = (test/100)*20;
projectMarks = (project/100)*20;
finalExamMarks = (finalExam/100)*40;
totalMarks = tutorialMarks + assignmentMarks + testMarks + projectMarks + finalExamMarks;
System.out.println("Your ID is: " + id);
int marksTotal= (int) Math.round(totalMarks);
System.out.printf("Your total marks is %d%%\n", marksTotal);
if(marksTotal < 100 && marksTotal >= 80) {
System.out.println("Your grade is A");
}
else if (marksTotal >= 70) {
System.out.println("Your grade is B+");
}
else if (marksTotal >= 65) {
System.out.println("Your grade is B");
}
else if (marksTotal >= 55) {
System.out.println("Your grade is C+");
}
else if (marksTotal >= 50) {
System.out.println("Your grade is C");
}
else if (marksTotal >= 40) {
System.out.println("Your grade is D");
}
else if (marksTotal >= 0) {
System.out.println("Your grade is F");
}
else {
System.out.println("Invalid grade!");
}
if (marksTotal > 77) {
int gradeA = A - marksTotal;
if (gradeA == 1){
System.out.printf("Total marks: %.1f. After round up is %d. %d-%d is %d mark. Borderline\n",totalMarks,marksTotal,A,marksTotal,gradeA);
}
else if (gradeA != 1 && gradeA != 0){
System.out.printf("Total marks: %.1f. After round up is %d. %d-%d is %d mark. Not borderline\n",totalMarks,marksTotal,A,marksTotal,gradeA);
}
else {
System.out.printf("Total marks: %.1f. After round up is %d. %d-%d is %d mark.\n",totalMarks,marksTotal,A,marksTotal,gradeA);
}
}
else if (marksTotal > 67) {
int gradeBPlus = bPlus - marksTotal;
if (gradeBPlus == 1){
System.out.println("Total marks: " + totalMarks + ". After round up is " + marksTotal + ". " + bPlus + "-" + marksTotal + " is " + gradeBPlus + " mark. Borderline");
}
else if (gradeBPlus != 1 && gradeBPlus != 0){
System.out.println("Total marks: " + totalMarks + ". After round up is " + marksTotal + ". " + bPlus + "-" + marksTotal + " is " + gradeBPlus + " mark. Not borderline");
}
else {
System.out.println("Total marks: " + totalMarks + ". After round up is " + marksTotal + ". " + bPlus + "-" + marksTotal + " is " + gradeBPlus + " mark.");
}
}
else if (marksTotal > 62) {
int gradeB = B - marksTotal;
if (gradeB == 1){
System.out.println("Total marks: " + totalMarks + ". After round up is " + marksTotal + ". " + B + "-" + marksTotal + " is " + gradeB + " mark. Borderline");
}
else if (gradeB != 1 && gradeB != 0){
System.out.println("Total marks: " + totalMarks + ". After round up is " + marksTotal + ". " + B + "-" + marksTotal + " is " + gradeB + " mark. Not borderline");
}
else {
System.out.println("Total marks: " + totalMarks + ". After round up is " + marksTotal + ". " + B + "-" + marksTotal + " is " + gradeB + " mark.");
}
}
else if (marksTotal > 52) {
int gradeCPlus = cPlus - marksTotal;
if (gradeCPlus == 1){
System.out.println("Total marks: " + totalMarks + ". After round up is " + marksTotal + ". " + cPlus + "-" + marksTotal + " is " + gradeCPlus + " mark. Borderline");
}
else if (gradeCPlus != 1 && gradeCPlus != 0){
System.out.println("Total marks: " + totalMarks + ". After round up is " + marksTotal + ". " + cPlus + "-" + marksTotal + " is " + gradeCPlus + " mark. Not borderline");
}
else {
System.out.println("Total marks: " + totalMarks + ". After round up is " + marksTotal + ". " + cPlus + "-" + marksTotal + " is " + gradeCPlus + " mark.");
}
}
else if (marksTotal > 47) {
int gradeC = C - marksTotal;
if (gradeC == 1){
System.out.println("Total marks: " + totalMarks + ". After round up is " + marksTotal + ". " + C + "-" + marksTotal + " is " + gradeC + " mark. Borderline");
}
else if (gradeC != 1 && gradeC != 0){
System.out.println("Total marks: " + totalMarks + ". After round up is " + marksTotal + ". " + C + "-" + marksTotal + " is " + gradeC + " mark. Not borderline");
}
else {
System.out.println("Total marks: " + totalMarks + ". After round up is " + marksTotal + ". " + C + "-" + marksTotal + " is " + gradeC + " mark.");
}
}
else if (marksTotal > 37) {
int gradeD = D - marksTotal;
if (gradeD == 1){
System.out.println("Total marks: " + totalMarks + ". After round up is " + marksTotal + ". " + D + "-" + marksTotal + " is " + gradeD + " mark. Borderline");
}
else if (gradeD != 1 && gradeD != 0){
System.out.println("Total marks: " + totalMarks + ". After round up is " + marksTotal + ". " + D + "-" + marksTotal + " is " + gradeD + " mark. Not borderline");
}
else {
System.out.println("Total marks: " + totalMarks + ". After round up is " + marksTotal + ". " + D + "-" + marksTotal + " is " + gradeD + " mark.");
}
}
else {
System.out.print("Invalid total marks!");
}
}
}while(finalExam > 100 || finalExam < 0);
}
}while(project > 100 || project < 0);
}
}while(test > 100 || test < 0);
}
}while(assignment > 100 || assignment < 0);
}
}while(tutorial > 100 || tutorial < 0);
}
}while(id.isEmpty());
System.out.println("Would you like to enter another student's mark? [Y/N]");
reenter = input.next();
enter = reenter.toLowerCase();
while(!"Y".equals(enter) || !"N".equals(enter)) {
System.out.println("Invalid input. Please enter again!\nWould you like to enter another student's mark? [Y/N]");
reenterAgain = input.next();
enterAgain = reenterAgain.toLowerCase();
}
}while(enterAgain.equalsIgnoreCase("Y"));
}//end main
}//end class
Here is the particular part that I am having a problem with.
System.out.println("Would you like to enter another student's mark? [Y/N]");
reenter = input.next();
enter = reenter.toLowerCase();
while(!"Y".equals(enter) || !"N".equals(enter)) {
System.out.println("Invalid input. Please enter again!\nWould you like to enter another student's mark? [Y/N]");
reenterAgain = input.next();
enterAgain = reenterAgain.toLowerCase();
}
I need to first ask the user to input how many problems they want to do. Then generate the first, then the second after they answer the first and so on.
public static void main(String[] args) {
int number1 = (int) (Math.random() * 40 + 10), number2 = (int) (Math.random() * 40 + 10), uanswer, ianswer, counter, icounter,
acounter, counter1, ui, aacounter, bcounter;
Scanner input = new Scanner(System.in);
System.out.println("How many problems do you want to do?");
ui = input.nextInt();
counter = 1;
icounter = 1;
acounter = counter + icounter;
{
System.out.print("What is " + number1 + " + " + number2 + "? ");
}
uanswer = input.nextInt();
ianswer = number1 + number2;
while (counter < 10000 && icounter < 1000 && acounter < 1000 && number1
+ number2 != uanswer) {
System.out.println("Incorrect, the answer is "
+ ianswer + ", " + icounter + " out of " + icounter + " incorrect. Try again?");
icounter++;
acounter++;
uanswer = input.nextInt();
}
if (ianswer == ianswer) {
aacounter = acounter - 1;
bcounter = icounter - 1;
System.out.println("Correct, the answer is " + ianswer
+ ", " + counter + " out of " + aacounter + " correct, "
+ bcounter + " out of " + aacounter + " incorrect.");
}
}
With my current code, I only see one problem, even though I asked for 2 or more problems at the beginning.
You need to add a loop around this statement:
System.out.print("What is " + number1 + " + " + number2 + "? ");
like:
for(int i=0; i<ui; i++){
System.out.print("What is " + number1 + " + " + number2 + "? ");
...
I have a segment of code that splits a string into tokens and prints them each out on a new line. I am having a hard time writing a code that determines if a word is a reserved word or not. I have to print "Reserved word is: " if the word is a java keyword, otherwise print "Current word is: ". Here is my code so far:
package projectweek3;
/**
*
* Name -
* Email Address -
* Date -
*
*/
public class Week3Project {
final static String program = "/*\n" +
" * To change this license header, choose License Headers in Project Properties.\n" +
" * To change this template file, choose Tools | Templates\n" +
" * and open the template in the editor.\n" +
" */\n" +
"package testapplication2;\n" +
"\n" +
"import java.util.Scanner;\n" +
"\n" +
"/**\n" +
" *\n" +
" * #author james\n" +
" */\n" +
"public class TestApplication2 {\n" +
"\n" +
" /**\n" +
" * #param args the command line arguments\n" +
" */\n" +
" public static void main(String[] args) {\n" +
" Scanner input = new Scanner(System.in);\n" +
" \n" +
" System.out.println(\"Enter integer #1\");\n" +
" int num1 = input.nextInt();\n" +
" \n" +
" System.out.println(\"Enter integer #2\");\n" +
" int num2 = input.nextInt();\n" +
" \n" +
" System.out.println(\"Enter integer #3\");\n" +
" int num3 = input.nextInt();\n" +
" \n" +
" System.out.println(\"Enter integer #4\");\n" +
" int num4 = input.nextInt();\n" +
" \n" +
" System.out.println(\"Enter integer #5\");\n" +
" int num5 = input.nextInt();\n" +
" \n" +
" //determine the sum\n" +
" int sum = num1 + num2 + num3 + num4 + num5;\n" +
" \n" +
" //this is helpful to make sure your sum is correct\n" +
" System.out.println(\"The sum is: \" + sum);\n" +
" \n" +
" //why doesn't this generate the sum correctly\n" +
" double average = sum / 5;\n" +
" \n" +
" //The average, lets hope its right...\n" +
" System.out.println(\"The average of your numbers is: \" + average);\n" +
" \n" +
" }\n" +
" \n" +
"}\n" +
"";
**public static void main(String[] args)
{
String str = program;
String s = "";
for (int i = 0; i < str.length(); i++) {
s += str.charAt(i) + "";
if (str.charAt(i) == ' ' || str.charAt(i) == '\t' || str.charAt(i) == '\n' || (str.charAt(i) == ' ' && str.charAt(i) == '\n')) {
String currentWord = s.toString();
String res = "int";
if (currentWord.equals(res)) {
System.out.println("Reserved word is: [" + currentWord + "]");
}
else {
System.out.println("Current word is: [" + currentWord + "]");
}
s = "";//Clear the string to get it ready to build next token.
}
}**
I would reconsider the way you're looping through the "program."
Instead of going through character by character, use the Java String.split() function.
String program = "int num1 = input.nextInt();\n";
String[] words = program.split("[\\n\\s\\t]");
for (String word : words) {
System.out.println(word);
}
Output:
int
num1
=
input.nextInt();
EDIT:
Since you can't use String.split(), your looping solution looks good. To check if the current word is reserved, try using Set.contains().
Set<String> reserved = new HashSet<>();
reserved.add("int");
// ...
if reserved.contains(word) {
System.out.println("Reserved word is: " + word);
} else {
System.out.println("Current word is: " + word);
}
That is, assuming you're allowed to use Set.
I'm in my first programming class; can anyone help me understand why I can't print my last line please?
package program4;
import java.util.*;
public class Program4 {
public static void main(String[] args) {
int a, b, c, numComparisons;
String comparisons = "Comparisons for triangleType determination: ";
Scanner scan = new Scanner(System.in);
for (int i = 0; i < 7; i++) {
}
String triangleType = "";
System.out.print("Enter 3 positive integer lengths for the sides of a "
+ "triangle:");
a = scan.nextInt();
b = scan.nextInt();
c = scan.nextInt();
System.out.println("The input lengths are: a = " + a + ", b = " + b + ", and"
+ " c = " + c + "");
if ((a + b < c) || (b + c < a) || (a + c < b)) {
System.out.print("There is no triangle with sides " + a + ", " + b + " and "
+ "" + c + ".");
} else {
numComparisons = 1;
comparisons += "a==b";
if (a == b) {
comparisons += "(T)" + "(b==c)";
numComparisons++;
if (b == c) {
comparisons += "(T)";
triangleType = "Equilateral";
}
} else {
comparisons += "(F)";
if (a == c) {
comparisons += "(T)";
triangleType = "Isosceles";
} else {
comparisons += "b==c";
numComparisons++;
comparisons += "(F)";
if (b == c) {
triangleType = "Isosceles";
} else {
comparisons += "a==c";
numComparisons++;
comparisons += "(F)";
triangleType = "Scalene";
}
}
}
System.out.printf("" + comparisons + (""));
System.out.printf("numComparisons = " + numComparisons);
System.out.println("The triangles with sides " + a + ", "
+ " + b + ", and " + c + ", is + triangleType + ");
}
}
}
Your last line syntax is pretty messed up.
this
System.out.println("The triangles with sides " + a + ", "
+ " + b + ", and " + c + ", is + triangleType + ");
should be
System.out.println("The triangles with sides " + a + ", "
+ b + ", and " + c + ", is " + triangleType);
System.out.println("The triangles with sides " + a + ", "
+ " + b + ", and " + c + ", is + triangleType + ");
What IDE/editor are you using? It should should show you the errors here. This should be
System.out.println("The triangles with sides " + a + ", "
+ b + ", and " + c + ", is" + triangleType);
This is better
how can I initialize all of my variables more easier?
is there a calculus package?
is there a more efficient solution overall?
import java.util.Scanner;
public class limits {
public static void main(String[] args)
{
String introMessage = " ***Calculus: Limits***" + "\n"
+ "This application uses the method of exhaustion" + "\n"
+ "to test limits. You enter the number that x" + "\n"
+ "approches and this program will give you three" + "\n"
+ "numbers on either side of the limit showing" + "\n"
+ "closer approximations of the limit.";
System.out.println(introMessage);
System.out.println();
String polynomialMessage = "Our function: " + "\n"
+ " lim f(x) = x^2 + x + 1 = L" + "\n"
+ "x -> a";
System.out.println(polynomialMessage);
System.out.println();
System.out.println("As x approaches a, what will our limit L be?");
System.out.println();
can I initialize all of these at once?
// initialize variables
double belowAOne = 0.0;
double belowATwo = 0.0;
double belowAThree = 0.0;
double belowAFour = 0.0;
double aboveAOne = 0.0;
double aboveATwo = 0.0;
double aboveAThree = 0.0;
double aboveAFour = 0.0;
double totalBAOne = 0.0;
double totalBATwo = 0.0;
double totalBAThree = 0.0;
double totalBAFour = 0.0;
double totalAAOne = 0.0;
double totalAATwo = 0.0;
double totalAAThree = 0.0;
double totalAAFour = 0.0;
double L = 0;
// create a Scanner object named sc
Scanner sc = new Scanner(System.in);
// perform invoice calculations until choice isn't equal to "y" or "Y"
String choice = "y";
while (!choice.equalsIgnoreCase("n"))
{
System.out.println("Please enter a whole number between 1 and 10 for a: ");
double a = sc.nextDouble();
if (a > 0 && a <=10 )
{
// calculate L
L = (a*a) + a + 1;
// create values that approaches a
belowAOne = a - .5;
belowATwo = a - .1;
belowAThree = a - .01;
belowAFour = a - .001;
aboveAOne = a + .5;
aboveATwo = a + .1;
aboveAThree = a + .01;
aboveAFour = a + .001;
totalBAOne = (belowAOne * belowAOne) + belowAOne + 1;
totalBATwo = (belowATwo * belowATwo) + belowATwo + 1;
totalBAThree = (belowAThree * belowAThree) + belowAThree + 1;
totalBAFour = (belowAFour * belowAFour) + belowAFour + 1;
totalAAOne = (aboveAOne * aboveAOne) + aboveAOne + 1;
totalAATwo = (aboveATwo * aboveATwo) + aboveATwo + 1;
totalAAThree = (aboveAThree * aboveAThree) + aboveAThree + 1;
totalAAFour = (aboveAFour * aboveAFour) + aboveAFour + 1;
String chart = " x " + "x^2 + x + 1" + "\n"
+ "---------+--------------" + "\n"
+ " " + belowAOne + " : " + totalBAOne + "\n"
+ " " + belowATwo + " : " + totalBATwo + "\n"
+ " " + belowAThree + " : " + totalBAThree + "\n"
+ " " + belowAFour + " : " + totalBAFour + "\n"
+ " " + " a " + " : " + "L" + "\n"
+ " " + aboveAFour + " : " + totalAAFour + "\n"
+ " " + aboveAThree + " : " + totalAAThree + "\n"
+ " " + aboveATwo + " : " + totalAATwo + "\n"
+ " " + aboveAOne + " : " + totalAAOne + "\n";
System.out.println();
System.out.println();
System.out.println();
System.out.println(chart);
System.out.println();
System.out.println("As X approaches " + a + ", our guess "
+ "for L is: " + L);
System.out.println();
// end the program
choice = "n";
}
else
{
System.out.println();
System.out.println("***Invalid Entry***");
System.out.println();
}
}
}
}
Is there a better way to write this program?
or you could use a loop instead like this:
double[] belowA = {a - .5, a - .1, a - .01, a - .001};
double[] totalBA = new double[4];
for(int i = 0; i < 4; ++i) {
totalBA[i] = (belowA[i] * belowA[i]) + belowA[i] + 1;
}
also with a StringBuilder / StringBuffer
and finally you could create a method to handle below/above in the same way without duplicated code