I am a complete beginner and hardly even know the basics of Java (I've only been in the class for two weeks). My first assignment is to calculate the heat index based on the current temperature and current humidity given by the user, in java using Eclipse. I have come up with a code, however, to no avail. My code does ask users to input the temperature and humidity, but it does not print out the results. I provided the UML diagram I was required to use to build the code that way you have a better understanding of why I did what I did. Ultimately, I think my problem lies somewhere in the process of passing values to and from different methods... Is there anyone who would be willing to take a look and possibly guide me in the right direction?
import java.util.Scanner;
public class HeatIndexCalculator1 {
private int temperature;
private double humidity;
private double heatIndex;
public static void main(String args[]) {
//Get current temp and humidity from user
Scanner input = new Scanner(System.in);
System.out.printf("Please enter the current temperature in degrees Fahrenheit: ");
int currentTemp = input.nextInt();
System.out.printf("\nPlease enter the current humidity as a percentage: ");
double currentHumidity = input.nextDouble();
}
private double calculateHeatIndex ( int currentTemp, double currentHumidity ) {
//Setting parameters for Function
int temperature = currentTemp;
double humidity = currentHumidity;
double answer;
final double C1 = -42.379;
final double C2 = 2.04901523;
final double C3 = 10.14333127;
final double C4 = -0.22475541;
final double C5 = -.00683783;
final double C6 = -5.481717E-2;
final double C7 = 1.22874E-3;
final double C8 = 8.5282E-4;
final double C9 = -1.99E-6;
int T = temperature;
double R = humidity;
double T2 = temperature * temperature;
double R2 = humidity * humidity;
//Function of Calculating Heat Index
double answer = C1 + (C2 * T) + (C3 * R) + (C4 * T * R) + (C5 * T2) + (C6 * R2) + (C7 * T2 * R) + (C8 * T * R2) + (C9 * T2 * R2);
return answer;
}
private void printHeatIndex( int currentTemp, double currentHumidity, double calculatedHeatIndex) {
double calculatedHeatIndex = answer;
//Print Heat Index
System.out.println("\nAt a temperature of" + currentTemp + "and a humidity of" + currentHumidity + "percent . . .\n");
System.out.println("\nIt feels like:" + calculatedHeatIndex + "F");
}
}
You need to change your main method as per below:
public static void main(String args[]) {
//Get current temp and humidity from user
Scanner input = new Scanner(System.in);
System.out.printf("Please enter the current temperature in degrees fahrenheit: ");
int currentTemp = input.nextInt();
System.out.printf("\nPlease enter the current humidity as a percentage: ");
double currentHumidity = input.nextDouble();
//Creating object of HeatIndexCalculator class
HeatIndexCalculator1 heatIndex = new HeatIndexCalculator1();
double x = heatIndex.calculateHeatIndex(..,..);
heatIndex.printHeatIndex(currentTemp,currentHumidity,x);
}
Also, compiler may give your error because the methods in HeatIndexCalculator1 class are private, if you change them to public it works.
Related
I have quickly made a small program that converts Fahrenheit into Celsius and Celsius into Fahrenheit but when I try to use the one variable it doesn't do all of the steps. I can understand why but I cannot figure out how I can prevent it from only doing the last operation because I want it to do all of the operations in order.
As far as im aware if I use the same variable for all of the operations such as the -32, *5 and /9 it will only do the last one because I have used = to assign that as the value but I am unsure whether this is the reason or not, any help would be appreciated, thanks.
public class TempConversion{
//FahrenheitSteps
private int Fahrenheit;
private int FahrenheitA;
private int FahrenheitB;
private int FahrenheitC;
//CelciusSteps
private int Celcius;
private int CelciusA;
private int CelciusB;
private int CelciusC;
//Constructor
public TempConversion(){
Fahrenheit = 0;
Celcius = 0;
}
//Convert Fahrenheit to celcius
public void FahrenheitToCelcius(int Fahren){
CelciusA = Fahren - 32;
CelciusB = CelciusA * 5;
CelciusC = CelciusB / 9;
System.out.println(CelciusC + " Is the celcius equivalent");
}
//Convert Celcius to fahrenheit
public void CelciusToFahrenheit(int Celc){
FahrenheitA = Celc * 9;
FahrenheitB = FahrenheitA / 5;
FahrenheitC = FahrenheitB + 32;
System.out.println(FahrenheitC + " Is the fahrenheit equivalent");
}
}
You can combine the following three lines:
CelciusA = Fahren - 32;
CelciusB = CelciusA * 5;
CelciusC = CelciusB / 9;
into
Celcius = (Fahren-32) * 5 / 9;
The same thing can be done for your celciusToFahrenheit method.
There shouldn't be an issue performing multiple math operations on one line as long as you remember the order of operations.
You can use an example like this https://www.programmingsimplified.com/java/source-code/java-program-to-convert-fahrenheit-to-celsius
And replace Scanner with your value.
import java.util.*;
class FahrenheitToCelsius {
public static void main(String[] args) {
float temperature;
Scanner in = new Scanner(System.in);
System.out.println("Enter temperature in Fahrenheit");
temperature = in.nextInt();
temperature = ((temperature - 32)*5)/9;
System.out.println("temperature in Celsius = " + temperature);
}
}
TL:DR program compiles but results do not print out
Hi, I am working on this assignment for my 1st year programming class. Here is the question:
: In this problem, you need to compute compound interest for various interest rates and various time periods.
More precisely, this means that given an amount to invest and an interest rate (a specific amount above the prime interest
rate, which for this assignment is 1.00 percent), compute the amount of money an individual would have after n years.
The formula to calculate this is given by:
final amount = amount(1.0 + (prime+rate/100.0)^n)
we need to invoke another class in this assignment from a prebuilt so the class they gave us was
import java.util.*;
//DO NOT PUT OTHER METHODS IN THIS CLASS or make any changes to it
public class UserInteraction
{
public String getStringValueFromUser(String message)
{
String value = "";
Scanner input = new Scanner(System.in);
System.out.print(message + " : ");
value = input.nextLine();
return value;
}
//DO NOT PUT OTHER METHODS IN THIS CLASS or make any changes to it
public double getDoubleValueFromUser(String message)
{
double value = 0;
Scanner input = new Scanner(System.in);
System.out.print(message + " : ");
value = input.nextDouble();
return value;
}
//DO NOT PUT OTHER METHODS IN THIS CLASS or make any changes to it
public int getIntValueFromUser(String message)
{
int value = 0;
Scanner input = new Scanner(System.in);
System.out.print(message + " : ");
value = input.nextInt();
return value;
}
//DO NOT PUT OTHER METHODS IN THIS CLASS or make any changes to it
}
and my code is here
import java.util.*;
public class InterestRate
{
UserInteraction input = new UserInteraction();
public static void main (String[] args)
{
InterestRate program = new InterestRate();
program.execute();
}
void execute()
{
double initial_money = input.getDoubleValueFromUser("Your initial amount");
double prime_rate = input.getDoubleValueFromUser("Enter the Prime Rate");
double InterestRate = input.getDoubleValueFromUser("Enter the Interest Rate as a value above prime");
double time = input.getDoubleValueFromUser("Enter the length of the investment in years to a decimal");
double InterestRate2 = input.getDoubleValueFromUser("Enter the second Interest Rate as a value above prime");
Calculations (initial_money, prime_rate, InterestRate, time);
Calculations2 (initial_money, prime_rate, InterestRate2, time);
}
void finaltotal(double totalrate4, double totalrate4a, double initial_money, double prime_rate, double InterestRate, double time, double InterestRate2)
{
double final_amount = (initial_money * totalrate4);
double final_amounta = (initial_money * totalrate4a);
printWinnings(final_amount, initial_money, InterestRate, prime_rate, final_amounta, time);
}
double Calculations(double initial_money, double prime_rate, double InterestRate, double time)
{
double totalrate = prime_rate + InterestRate;
double totalrate2 = totalrate / 100.0;
double totalrate3 = totalrate2 + 1.0;
double totalrate4 = Math.pow(time, totalrate3);
return totalrate4;
}
double Calculations2(double initial_money, double prime_rate, double InterestRate2, double time)
{
double totalrate1a = prime_rate + InterestRate2;
double totalrate2a = totalrate1a / 100.0;
double totalrate3a = totalrate2a + 1.0;
double totalrate4a = Math.pow(time, totalrate3a);
//double final_amounta = initial_money * totalrate4;
return totalrate4a;
}
void printWinnings(double final_amount, double initial_money, double prime_rate, double InterestRate, double time, double final_amounta)
{
System.out.println(" The amount of money you can expect to get over the " + time + " years you invest will be " + final_amount + " with your initial investment of " + initial_money + " at an rate of " + InterestRate);
}
}
So I typed it all up, and the program compiles, but when I run it, nothing prints out.
Technically you didn't ask a question you made a statement. I assume you meant to ask why your program does not produce any output of results after the initial prompts for input.
If you follow the execution of your code it goes like this:
The main method is executed first:
public static void main(String[] args)
{
InterestRate program = new InterestRate();
program.execute();
}
As you can see your execute method is called from main, so now the execution goes here:
void execute() {
double initial_money = input
.getDoubleValueFromUser("Your initial amount");
double prime_rate = input
.getDoubleValueFromUser("Enter the Prime Rate");
double InterestRate = input
.getDoubleValueFromUser("Enter the Interest Rate as a value above prime");
double time = input
.getDoubleValueFromUser("Enter the length of the investment in years to a decimal");
double InterestRate2 = input
.getDoubleValueFromUser("Enter the second Interest Rate as a value above prime");
Calculations(initial_money, prime_rate, InterestRate, time);
Calculations2(initial_money, prime_rate, InterestRate2, time);
}
Before I move on I just want to mention that naming methods so that they begin with a capital letter violates accepted Java standard naming practice. Methods are always named using camel case and starting with lowercase. Similarly, naming variables in this manner also violates the naming conventions of Java. For example, InterestRate should be interestRate because otherwise it reads like a class name. Generally only constants use underscores between words so things like initial_money would be more consistent with naming convention if written as initialMoney.
Getting back to your code - from where we left off your logic flows into Calculations and Calculations2:
double Calculations(double initial_money, double prime_rate,
double InterestRate, double time) {
double totalrate = prime_rate + InterestRate;
double totalrate2 = totalrate / 100.0;
double totalrate3 = totalrate2 + 1.0;
double totalrate4 = Math.pow(time, totalrate3);
return totalrate4;
}
double Calculations2(double initial_money, double prime_rate,
double InterestRate2, double time) {
double totalrate1a = prime_rate + InterestRate2;
double totalrate2a = totalrate1a / 100.0;
double totalrate3a = totalrate2a + 1.0;
double totalrate4a = Math.pow(time, totalrate3a);
// double final_amounta = initial_money * totalrate4;
return totalrate4a;
}
Now you will notice none of these methods have any calls to System.out.print or System.out.println. This is why you don't see any output.
import java.util.Scanner ;
public class CollinsHealthCalculator {
double ACTIVITY_FACTOR = 1.375;
public static void main (String[] args) {
newHealthCalcDescription ();
Scanner keyboard = new Scanner (System.in);
System.out.println ("What is your weight in pounds? ");
double weightlb = keyboard.nextDouble ();
System.out.println ("What is your height in inches? ");
double heightin = keyboard.nextDouble ();
System.out.println ("What is your age in years? ");
double ageYears = keyboard.nextDouble ();
double WEIGHT_KILOGRAMS = weightlb / 2.2;
double HEIGHT_METERS = heightin * .0254;
double weightkg = WEIGHT_KILOGRAMS;
double heightm = HEIGHT_METERS;
double computingBMI (BMI, weightkg, heightm);
maleBMR (heightm, weightkg, ageYears);
femaleBMR (heightm, weightkg, ageYears);
showResults (BMI, caloriesm, caloriesf);
public static newHealthCalcDescription () {
System.out.println("This calculator will determine your BMI "
+ "(Body Mass Index). While also it will determine the amount "
+ "of calories needed to maintain weight.");
}
//Computing the BMI
public static void computingBMI (double BMI, double weightkg, double heightm){
BMI = weightkg/(Math.pow(heightm, 2));
}
//Computing BMR for male and female
public static void maleBMR (double heightm, double weightkg, double ageYears) {
double HEIGHT_CENTIMETERS = heightm * 100;
double heightcm = HEIGHT_CENTIMETERS ;
double BMRForMales = 13.397 * weightkg + 4.799 * heightcm - 5.677 * ageYears + 88.362;
double caloriesm = Math.round(BMRForMales * 1.375);
}
public static void femaleBMR (double heightm, double weightkg, double ageYears) {
double HEIGHT_CENTIMETERS = heightm * 100;
double heightcm = HEIGHT_CENTIMETERS ;
double BMRForFemales = 9.247 * weightkg + 3.098 * heightcm - 4.330 * ageYears + 447.593;
double caloriesf = Math.round(BMRForFemales * 1.375);
}
public static void showResults (double BMI, double caloriesm, double caloriesf) {
//Show results
System.out.printf ("%nYour BMI is: %7.1f", BMI);
System.out.println ("A BMI between 18.5 to 24.9 is considered normal.");
System.out.println ();
System.out.println ("To maintain current weight:");
System.out.print ("Men need to eat " + caloriesm);
System.out.println (" calories per day.");
System.out.print ("Females need to eat " + caloriesf);
System.out.println (" calories per day.");
}
}
I'm trying to get the code to pass down statements but I'm new to programming and have no clue on how to go about getting method passed down to another method. I've tried researching everywhere but I've had little luck in finding any help. Please help so I can make my programm functional I'm excited to learn just need help.
You can try giving the variables the global scope(outside the method). You may learn about it here.
When you declare a variable inside a method (i.e. code block), it is local to that block. So you cannot use that variable in any other method. Here the best option for you to do is to declare the variable, i.e. like weightkg etc as class variables.
You can change the return type of the methods from void to double and store the returned result and send the results to other methods.
for eg.
public static double computingBMI (double BMI, double weightkg, double heightm){
return weightkg/(Math.pow(heightm, 2));
}
fix me plz. i get multiple error messages
"variable airSpeed_km might not have been initialized"
"variable width might not have been initialized"
"variable length might not have been initialized"
import java.util.Scanner;
public class V4_________1{
public static void main (String args[])
{
Scanner keyboard = new Scanner(System.in);
double KNOTS_TO_KMPHR;
double airSpeed_km;
double airSpeed_knots;
double width;
double length;
***// need to do something in the main but not sure what exactly***
airSpeed_knots = keyboard.nextDouble();
System.out.println("what is your current airspeed in knots?");
System.out.println("your current airspeed in km is: " + airSpeed_km + "your holding pattern width is: " + width + "your holding patter length is: " + length);
}
public static double getAirSpeed(double airSpeed_knots, double KNOTS_TO_KMPHR, double airSpeed_km)
{
KNOTS_TO_KMPHR = 1.852;
airSpeed_km = airSpeed_knots * KNOTS_TO_KMPHR ;
return airSpeed_km;
}
public static double calcPatternWidth(double width, double airSpeed_km)
{
width = (airSpeed_km) / (60 * Math.PI) * 2;
return width;
}
public static double calcPatternLength(double airSpeed_km, double length)
{
length = (airSpeed_km) / (60 * Math.PI) * 2 + ((airSpeed_km) / 60);
return length;
}
}
You declare:
double airSpeed_km;
And after use it:
System.out.println("your current airspeed in km is: " + airSpeed_km + "your holding pattern width is: " + width + "your holding patter length is: " + length);
without any assignment. So you get an error, you can prevent this by giving it a default value of 0 for example.
double airSpeed_km = 0;
(same goes for your other errors)
In Java, the compiler gets upset if a variable even MIGHT be used without a value.
So it is best practice to always give a value to variables when you first declare them.
Since you normally don't know the value a variable will have at the time of declaration, it is common practice to give it a value of zero.
So your declarations should look like this:
double KNOTS_TO_KMPHR=0;
double airSpeed_km=0;
double airSpeed_knots=0;
double width=0;
double length=0;
This will take care of all your "[] might not have been initialized" compiler errors.
Your code is in correct. Your cannot set variable when you pass them into a function. See both approaches and understand what going on.
You can do this:
public static void main (String args[])
{
Scanner keyboard = new Scanner(System.in);
double KNOTS_TO_KMPHR=1.852;
double airSpeed_knots;
System.out.println("what is your current airspeed in knots?");
airSpeed_knots = keyboard.nextDouble();
System.out.println("your current airspeed in km is: " + getAirSpeed(airSpeed_knots) + "your holding pattern width is: " + calcPatternWidth(getAirSpeed(airSpeed_knots)) + "your holding patter length is: " + calcPatternLength(getAirSpeed(airSpeed_knots));
}
public static double getAirSpeed(double airSpeed_knots)
{
return airSpeed_knots * KNOTS_TO_KMPHR ;
}
public static double calcPatternWidth(double airSpeed_km)
{
return (airSpeed_km) / (60 * Math.PI) * 2;
}
public static double calcPatternLength(double airSpeed_km)
{
return (airSpeed_km) / (60 * Math.PI) * 2 + ((airSpeed_km) / 60);
}
Or do this if you want to set variables:
public static void main (String args[])
{
Scanner keyboard = new Scanner(System.in);
double KNOTS_TO_KMPHR=1.852;
double airSpeed_knots;
System.out.println("what is your current airspeed in knots?");
airSpeed_knots = keyboard.nextDouble();
double airSpeed_km=getAirSpeed(airSpeed_knots);
double width=calcPatternWidth(airSpeed_km);
double length= calcPatternLength(airSpeed_km);
System.out.println("your current airspeed in km is: " + airSpeed_km + "your holding pattern width is: " + width + "your holding patter length is: " + length);
}
public static double getAirSpeed(double airSpeed_knots)
{
return airSpeed_knots * KNOTS_TO_KMPHR ;
}
public static double calcPatternWidth(double airSpeed_km)
{
return (airSpeed_km) / (60 * Math.PI) * 2;
}
public static double calcPatternLength(double airSpeed_km)
{
return (airSpeed_km) / (60 * Math.PI) * 2 + ((airSpeed_km) / 60);
}
I am having an issue with a method returning to the main method. It is saying that amount in "return amount" cannot be resolved to a variable. Where am I off on this??
This is the message I get:
Multiple markers at this line
- Void methods cannot return a
value
- amount cannot be resolved to a
variable
Here is the code:
import java.util.Scanner;
public class Investment {
public static void main(String[]args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the amount invested: ");
double amount = input.nextDouble();
System.out.print("Enter the annual interest rate: ");
double interest = input.nextDouble();
int years = 30;
System.out.print(futureInvestmentValue(amount, interest, years)); //Enter output for table
}
public static double futureInvestmentValue(double amount, double interest, int years) {
double monthlyInterest = interest/1200;
double temp;
double count = 1;
while (count < years)
temp = amount * (Math.pow(1 + monthlyInterest,years *12));
amount = temp;
System.out.print((count + 1) + " " + temp);
}
{
return amount;
}
}
You curly braces are not correct. The compiler - and me - was confused about that.
This should work (at least syntactically):
import java.util.Scanner;
public class Investment {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the amount invested: ");
double amount = input.nextDouble();
System.out.print("Enter the annual interest rate: ");
double interest = input.nextDouble();
int years = 30;
System.out.print(futureInvestmentValue(amount, interest, years));
}
public static double futureInvestmentValue(
double amount, double interest, int years) {
double monthlyInterest = interest / 1200;
double temp = 0;
double count = 1;
while (count < years)
temp = amount * (Math.pow(1 + monthlyInterest, years * 12));
amount = temp;
System.out.print((count + 1) + " " + temp);
return amount;
}
}
Remove amount from its own scope As a start. Also from the method futureInvestmentValue, you take in amount as an argument but the value is never modified so you're returning the same value being passed which is most likely not the desired outcome.
remove return amount from its own scope
the method futureInvestmentValue... You can't modify any of the parameters inside the method so you have to declare another variable besides amount inside the method (maybe it's the temp variable you keep using) and return that instead
when you return something, the return statement is always inside the method. Never outside it while inside its own braces (never seen this before...)
import java.util.Scanner;
public class Investment {
public static void main(String[]args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the amount invested: ");
double amount = input.nextDouble();
System.out.print("Enter the annual interest rate: ");
double interest = input.nextDouble();
int years = 30;
System.out.print(futureInvestmentValue(amount, interest, years)); //Enter output for table
}
public static double futureInvestmentValue(double amount, double interest, int years) {
double monthlyInterest = interest/1200;
double temp;
double count = 1;
while (count < years) {
temp = amount * (Math.pow(1 + monthlyInterest,years *12));
System.out.print((count + 1) + " " + temp);
}
return amount;
}
}