the left hand side of the assignment is not a variable? - java

I am trying to set this up so my tests will work but I keep getting an error about the left-hand side of the assignment is not a variable on the line starting with 'flunking.gpa...'. Any suggestions as to what i am doing wrong?
/**
* After we have added hours and quality points, we need to
* check that the gpa is (quality points) / hours
*/
#Test
public void gpa() {
flunking.gpa() = flunking.qualityPoints() / (double)flunking.hours();
assertEquals(flunking.gpa(), 0.0, DELTA);
}

You're attempting to assign a value to a method
flunking.gpa() = flunking.qualityPoints() / (double)flunking.hours();
As both qualityPoints and hours contain values within the flunking classs, there's should be no need for any assignment here, i.e. just have gpa return the calculated value in that class as required, e.g.
public double getGPA() {
return qualityPoints / (double)hours;
}

Perhaps there is a flunking.setGpa(newValue) or flunking.gpa(newValue) method to change the value? .gpa() just returns the value and it doesn't make sense to assign to it, technically speaking.

Related

java: Two Possible int Values: Score Tracker for a Game

I'm working on a project to create a Space Invaders style game:
I need to be able to keep score when a player hits the target (+1 point) or misses (-1 point).
I will call the recordScore() method when the player shoots at the alien entity, however, I am unsure how to set it up so that my value (int point) can either be 1 or -1.
This is what I currently have:
public void recordScore(int point){
if(/*an alien ship is hit*/){
point = 1;
}
else{
point = -1;
}
I will call the method as such:
//if there is a hit
recordScore(1)
//if there is no hit
recordScore(-1)
...which I feel is a hint but I feel like using the loop is incorrect as I feel that would not work with how the method will be called but I can't currently think of any other way to initialize the variable to two values (or fill the if clause).
This is thinking backwards:
public void recordScore(int point){
if(/*an alien ship is hit*/){
point = 1;
}
else{
point = -1;
}
You are trying to assign a value to a method parameter which is the exact opposite of what you should be doing. Remember that point is coming into this method with a value assigned and passed into it.
Instead use the value that the parameter holds to assign to the value of a class field, or in your case to increment or decrement the value of a field.
So something like:
public void recordScore(int point) {
score += point; // similar to score = score + point;
}
is closer to what you need. I have no idea if you have a score field and you may name it something else.
Note that you state:
I feel like using the loop is incorrect
If you're referring to to the if/else structure, please understand that this is not a loop but rather a program control flow statement or logic block. A loop would make code repeat itself, and examples include for loops and while loops.
If you will only want to increment or decrement score by 1, another option is to use a method that uses a boolean parameter. For example
public void attemptHit(boolean hitSuccessful) {
if (hitSuccessful) {
score++;
else {
score--;
}
}
and you'd call it like:
attemptHit(true); // for a successful hit
attemptHit(false); // for a miss
This will prevent nonsense but valid code from being created such as
recordScore(-23020);

Convert Height to Inches (Java)

I am having issues with the output of my code. Seems I am missing something in my method that I created... I had instructions to return the total number of inches. I places totInches after return and get an error stating that totInches is not a variable. Not certain what is missing here as I am only supposed to be creating a method. Most of this code was written and the only portion I was supposed to created was the second convertToInches method.. Any advice?
import java.util.Scanner;
public class FunctionOverloadToInches {
public static double convertToInches(double numFeet) {
return numFeet * 12.0;
}
public static double convertToInches(double numFeet, double numInches) {
return totInches * 12.0;
}
public static void main (String [] args) {
double totInches = 0.0;
totInches = convertToInches(4.0, 6.0);
System.out.println("4.0, 6.0 yields " + totInches);
totInches = convertToInches(5.9);
System.out.println("5.9 yields " + totInches);
return;
}
}
The variable totInches is not defined in the scope of your function:
public static double convertToInches(double numFeet, double numInches) {
return totInches * 12.0;
}
The only variables you can use in this function are the ones you create and the ones defined as formal parameters: numFeet and numInches. So you have to come up with an equation that takes numFeet and converts it to inches, taking into account the additional inches provided in numInches.
You declared the double variable "totInches" inside of your main method, but you are trying to access it inside of your "convertToInches" method. When declaring a variable in a particular method, that variable is ONLY accessible by that method. Your "convertToInches" knows of only two variables: numFeet and numInches, which you passed to it in the parameter. It then looks at your return statement, sees "totInches" and has no idea what it is.
I also don't understand what this is trying to do...
public static double convertToInches(double numFeet, double numInches) {
return totInches * 12.0;
}
Why are you passing it the double variables numFeet and numInches? The function isn't using them. I also don't understand why you need both the number of feet AND the number of inches if the method, by its name, is trying to convert something into inches.
public static double convertToInches(double numFeet, double numInches) {
return (numFeet * 12) + numInches;
This takes into account any variable amount entered by user for height of 5 feet 7 inches or 6 feet even

Java: calculating velocity of a skydiver

In Java, I am trying to implement the following equation for calculating the current velocity of a skydiver not neglecting air resistance.
v(t) = v(t-∆t) + (g - [(drag x crossArea x airDensity) / (2*mass)] *
v[(t-∆t)^2] ) * (∆t)
My problem is that I am not sure how to translate "v(t - ∆t)" into a code. Right now I have this method below, where as you can see I am using the method within itself to find the previous velocity. This has continued to result in a stack overflow error message, understandably.
(timeStep = ∆t)
public double calculateVelocity(double time){
double velocity;
velocity = calculateVelocity(time - timeStep)
+ (acceleration - ((drag * crossArea * airDensity)
/ (2 * massOfPerson))
* (calculateVelocity(time - timeStep)*(time * timeStep)))
* timeStep;
}
return velocity;
}
I am calling the above method in the method below. Assuming that the ending time = an int, will be the user input but written this way to be dynamic.
public void assignVelocitytoArrays(){
double currentTime = 0;
while(currentTime <= endingTime){
this.vFinal = calculateVelocity(currentTime);
currentTime += timeStep;
}
}
I would like to figure this out on my own, could someone give me a general direction? Is using a method within itself the right idea or am I completely off track?
The formula you want to implement is the recursive representation of a sequence, mathematiacally speaking.
Recursive sequences need a starting point, e.g.
v(0) = 0 (because a negative time does not make sense)
and a rule to calculate the next elements, e.g.
v(t) = v(t-∆t) + (g - [(drag x crossArea x airDensity) / (2*mass)] * v[(t-∆t)^2] ) * (∆t)
(btw: are you sure it has to be v([t-∆t]^2) instead of v([t-∆t])^2?)
So your approach to use recursion (calling a function within itself) to calculate a recursive sequence is correct.
In your implementation, you only forgot one detail: the starting point. How should your program know that v(0) is not defined be the rule, but by a definite value? So you must include it:
if(input value == starting point){
return starting point
}
else{
follow the rule
}
On a side note: you seem to be creating an ascending array of velocities. It would make sense to use the already calculated values in the array instead of recursion, so you don't have to calculate every step again and again.
This only works if you did indeed make a mistake in the rule.
double[] v = new double[maxTime/timeStep];
v[0] = 0; //starting point
for(int t = 1; t < maxSteps; t++){
v[t] = v[t-1] + (g - [(drag x crossArea x airDensity) / (2*mass)] * v[t-1]^2 ) * (∆t)
}

Method (correctly) declares variables as 0 but I don't understand why?

I have some code to look at for a class and I understand most of it, but am confused about this method. With the given code, wouldn't the return change always result in 0 since the last thing put in was that the totalOfItems and the totalGiven are 0.0. I was told that when running it that won't happen but I'd like to understand why. Can anyone help me?
public SelfCheckout () {
this.totalOfItems = 0.0;
this.totalGiven = 0.0;
}
/**
* This method will scan the item.
* #param amount The amount the items cost.
*/
public void scanItem (double amount){
this.totalOfItems = this.totalOfItems + amount;
}
/** The method will add the items scanned and return the total due.
*
* #return getTotalDue The getTotalDue is the total amount of items scanned.
*/
public double getTotalDue(){
return this.totalOfItems;
}
/** The method will show the amount that is received from the consumer.
*
*/
public void receivePayment(double amount){
this.totalGiven = this.totalGiven + amount;
}
/**The method will calculate the amount of change due.
*
*/
public double produceChange(){
double change = this.totalGiven - this.totalOfItems;
this.totalGiven = 0.0;
this.totalOfItems = 0.0;
return change;
Statements execute in order. Changes to totalGiven and totalOfItems won't change change after it has been computed.
For this:
double change = this.totalGiven - this.totalOfItems;
this.totalGiven = 0.0;
this.totalOfItems = 0.0;
return change;
first you assign a (non-zero) value to change, then you reset the original variables, and only then return the value of change. The variable values are copied to another variable and then reset.
The assumption I think is that at some point receivePayment and scanItem have already been called so they reassign the field variables to a number that is not zero. Then change is given. The transaction is closed after you have already computed change you reset the variables for the next transaction.

Calculating math in GPS format

I'm making a Android Application to calculate Math in GPS Format.
Example:
Given
N 48°44.(30x4) E 019°08.[(13x31)+16]
the App calculates it, and result is:
N 48°44.120 E 019°08.419
Is it possible to do this?
I searched for plugins and solutions, but it's all just for math strings like as "14 + 6".
I am assuming you are working in Java as it is tagged in your question.
You could create a new public class for your GPS coordinates, and store the actual value of the coordinate in the lowest division, which according to your example appears to be minutes or seconds. This allows you to store the value as an int or a double with whatever precision you wish. You could then create a set of private and public methods to complete your mathematical operations and others to display your values in the appropriate fashion:
public class GPSCoordinate {
private double verticalcoord;
private double horizontalcoord;
//Constructors
GPSCoordinate(){
setVertical(0);
setHorizontal(0);
}
GPSCoordinate(double vert, double horiz){
setVertical(vert);
setHorizontal(horiz);
}
//Display methods
public String verticalString(){
return ((int)verticalcoord / 60) + "°" + (verticalcoord - ((int)verticalcoord / 60) *60);
}
public String horizontalString(){
return ((int)horizontalcoord / 60) + "°" + (horizontalcoord - ((int)horizontalcoord / 60) *60);
}
//Setting Methods
public void setVertical(double x){
this.verticalcoord = x;
}
public void setHorizontal(double x){
this.horizontalcoord = x;
}
//Math Methods
public void addMinutesVertical(double x){
this.verticalcoord += x;
}
}
This will allow you to initiate an instance in your main code with a given GPS coordinate, and then you can call your math functions on it.
GPSCoordinate coord1 = new GPSCoordinate(567.23, 245);
coord1.addMinutesVertical(50);
coord1.otherMathFunction(50 * 30);
You will, of course, need to refine the above to make it fit your project. If this isn't helpful, please provide more specifics and I'll see if I can think of anything else that might fit what your looking for.
Can't you just substring the whole thing and search for the expression in the brackets? Then it's just a matter of simple calculation. If I understood the question correctly. The gps data doesn't look like an ordinary expression, so you can't appy math() directly.

Categories

Resources