I have a method:
#Override
public void onSensorChanged(SensorEvent event) {
float [] values = event.values;
float altitude;
altitude = SensorManager.getAltitude(SensorManager.PRESSURE_STANDARD_ATMOSPHERE,values[0]);
}
How can I access to the value of "altitude" from another class?
thanks for the help.
You can not, it looks like you are using java? and you are overriding a function so returning it may not be an option.
so an easy solution would be to declare altitude as a class variable, not in the function and provide a accessor/geter function.
class Someclass
{
private float altitude;
#Override
public void onSensorChanged(SensorEvent event)
{
float [] values = event.values;
altitude = SensorManager.getAltitude(SensorManager.PRESSURE_STANDARD_ATMOSPHERE,values[0]);
}
//function to get altitude
public float getAltitude()
{
return altitude;
}
}
Related
I have a class AsyncTask as an inner class to a fragment.
public class MyAsyncTask extends AsyncTask<String,double[],double[]> {
double posScoreb = 0.0;
double negScoreb= 0.0;
public AsyncResponse delegate = null;
#Override
protected void onPreExecute() {
progressBar.setVisibility(View.VISIBLE);}
#Override
protected double[] doInBackground(String... params) {
s = new SentiCalc(getActivity().getAssets());
s.setInput(String.valueOf(params[0]));
double[] set = new double[]{};
try {
set = s.getSenti();
} catch (IOException e) {
e.printStackTrace();
}
posScoreb = set[1];
negScoreb = set[2];
return new double[]{posScoreb,negScoreb};
}
#Override
protected void onPostExecute(double[] result) {
progressBar.setVisibility(View.GONE);
result[0] = posScoreb;
result[1] = negScoreb;
delegate.processFinish(result);
Toast.makeText(getActivity(), "Done",Toast.LENGTH_LONG).show();
}
}
I am calling it from a method saveNote() inside the same fragment.`
asyncTask = new MyAsyncTask() ;
asyncTask.delegate =this;
asyncTask.execute(content);
I want AsyncTask class to return values inside a result(double array) to saveNote method. As shown in this, How to get the result of OnPostExecute() to main activity because AsyncTask is a separate class?.
I created an interface and made my fragment class to implement it.
public interface AsyncResponse {
void processFinish(double[] output);}
As a result of which I am able to get the result inside a method processFinish
within the same fragment.
#Override
public void processFinish(double[] output) {
double posScore,negScore;
posScore = output[0];
negScore = output[1];
Toast.makeText(getActivity(),String.valueOf(posScore),Toast.LENGTH_LONG).show();
Toast.makeText(getActivity(), String.valueOf(negScore), Toast.LENGTH_LONG).show();
}
How can I get this result inside a saveNote method from which I am calling AsyncTask class?
So the part of my question is ''Modify getMoonName() and getMoonRadius() so they are given an array index value of the moon whose name or radius is required.''
I've tried adding moons[i].getRadius but then end up getting ''The variable i does not exist''. Here's the code.
PLANET CLASS
public class Planet
{
private float angle=0.01;
// add class member variables here
private String name;
private float radius;
private float distance;
private float speed;
private Moon[] moons;
// add constructor here
public Planet(String n, float r, float d, float s, Moon[] m)
{
this.name=n;
this.radius=r;
this.distance=d;
this.speed=s;
this.moons=m;
}
// add other methods here
public String getName()
{
return name;
}
public float getRadius()
{
return radius;
}
public float getDistance()
{
return distance;
}
public float getSpeed()
{
return speed;
}
public Moon[] getMoons()
{
return moons;
}
public void setRadius(float r)
{
this.radius=r;
}
public String getMoonName()
{
return moons[i].getName();
}
public float getMoonRadius()
{
return moons[i].getRadius();
}
public String toString()
{
int n=0;
for (int i=0; i<moons.length; i++)
{
n++;
}
return "Planet" + name + ("Radius: " +radius +"Distance: " +distance) +n +"moons.";
}
public void printMoons()
{
for (int i=0; i<moons.length; i++)
{
System.out.println(moons[i]);
}
}
// This will display the moon when other code is completed. You don't need to understand this code.
public void display()
{
angle=angle+(0.01*speed);
pushMatrix();
rotate(angle);
translate(distance,0);
fill(255, 255, 255);
ellipse(0, 0, radius*2, radius*2);
for(Moon moon: getMoons())
moon.display();
popMatrix();
}
}`
MOON CLASS
public class Moon
{
private float angle=0.01;
// add class member variables here
private String name;
private float radius;
private float distance;
private float speed;
private int orbitalPeriod;
// add constructor here
public Moon(String n, float r, float d, float s, int o)
{
this.name=n;
this.radius=r;
this.distance=d;
this.speed=s;
this.orbitalPeriod=o;
}
// add other methods here
public String getName()
{
return name;
}
public float getRadius()
{
return radius;
}
public float getDistance()
{
return distance;
}
public float getSpeed()
{
return speed;
}
public float getOrbitalPeriod()
{
return orbitalPeriod;
}
public void setName(String n)
{
this.name=n;
}
public void setOrbitalPeriod(int o)
{
this.orbitalPeriod=o;
}
public String toString()
{
return ("Moon : " +name +" "+"orbit="+orbitalPeriod);
}
// This will display the moon when other code is completed. You don't need to understand this code.
public void display()
{
angle=angle+(0.01*speed);
pushMatrix();
rotate(angle);
translate(distance, 0);
fill(149, 149, 149);
ellipse(0, 0, radius*2, radius*2);
popMatrix();
}
}
Let's look at this function:
public String getMoonName()
{
return moons[i].getName();
}
Where do you think the i variable is defined? Your instructions say to take an argument, but this function does not take any arguments.
As a small example, let's say I had this function:
public void printMessage(){
println("Hello!");
}
If I wanted to modify that function to take a parameter, I would have to add that to the method like this:
public void printMessage(String message){
println(message);
}
You have to do something similar with your getMoonName() function.
If you're still stuck, please post a small example like mine instead of your whole sketch, and we'll go from there.
I am trying to implement java interface in nativescript plugin (typescript)
but when i call view it gives me this error. cannot convert object to Landroid/view/textureview/surfacetexturelistener
I assume my typescript class is not implementing interface SurfaceTextureListener. But I added all 4 method needed.
here is working java code
public class FFmpegRecordActivity extends AppCompatActivity implements
TextureView.SurfaceTextureListener, View.OnClickListener {
...
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPreview = (FixedRatioCroppedTextureView) findViewById(R.id.camera_preview);
mCameraId = Camera.CameraInfo.CAMERA_FACING_FRONT;
// Switch width and height
mPreview.setPreviewSize(previewHeight, previewWidth);
mPreview.setCroppedSizeWeight(videoWidth, videoHeight);
mPreview.setSurfaceTextureListener(this);
}
#Override
public void onSurfaceTextureAvailable(final SurfaceTexture surface, int width, int height) {
startPreview(surface);
}
#Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
}
#Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
return true;
}
#Override
public void onSurfaceTextureUpdated(SurfaceTexture surface) {
}
...
}
Typescript code I am trying
export class NumberPicker extends view.View {
public _createUI() {
// this._android = new android.widget.NumberPicker(this._context);
this.mCameraId = android.hardware.Camera.CameraInfo.CAMERA_FACING_FRONT;
this._android = new com.github.crazyorr.ffmpegrecorder.FixedRatioCroppedTextureView(this._context);
this._android.setPreviewSize(100, 100);
this._android.setCroppedSizeWeight(100, 100);
this._android.setSurfaceTextureListener(this);
console.log("my-plugin - Android : _createUI")
};
public onSurfaceTextureAvailable(surface, width, height) {
this.startPreview(surface);
}
public onSurfaceTextureSizeChanged(surface, width, height) {
}
public onSurfaceTextureDestroyed(surface): Boolean {
return true;
}
public onSurfaceTextureUpdated(surface) {
}
}
I tried adding
declare var android:any;
// #Interfaces([android.view.TextureView.SurfaceTextureListener]) /* the interfaces that will be inherited by the resulting MyVersatileCopyWriter class */
export class NumberPicker extends view.View implements {
// interfaces:[android.view.TextureView.SurfaceTextureListener]
But all got error of cant find android in ts compiler
I believe you need to add implements android.view.TextureView.SurfaceTextureListener to your component
for the life of me I can't figure out what is wrong with this code, please help. I have three classes, GeometricObject, Octagon which extends GeometricObject and TestOctagon which is being used to test the Octagon class. When I run the TestOctagon class I get this error:
The method clone() from the type Object is not visible
Here is my code:
public abstract class GeometricObject {
private String color = "white";
private boolean filled;
protected GeometricObject() {
}
protected GeometricObject(String color, boolean filled) {
this.color = color;
this.filled = filled;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public boolean isFilled() {
return filled;
}
public void setFilled(boolean filled) {
this.filled = filled;
}
public abstract double getArea();
public abstract double getPerimeter();
}
import java.lang.Comparable;
import java.lang.Cloneable;
public class Octagon extends GeometricObject implements Comparable<Octagon>, Cloneable{
double side;
public Octagon() {
}
public Octagon(double side) {
this.side = side;
}
public Octagon(double side, String color, boolean filled) {
this.side = side;
setColor(color);
setFilled(filled);
}
public double getSide() {
return side;
}
public void setSide(double side) {
this.side = side;
}
public double getArea() {
return (2+4/Math.sqrt(2))*side*side;
}
public double getPerimeter() {
return 8*side;
}
#Override
public int compareTo(Octagon o) {
if (getArea() > o.getArea())
return 1;
else if (getArea() < o.getArea())
return -1;
else
return 0;
}
#Override
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
public class TestOctagon {
public static void main(String[] args) {
// TODO Auto-generated method stub
GeometricObject oc1 = new Octagon(5);
System.out.println(oc1.getArea());
System.out.println(oc1.getPerimeter());
GeometricObject oc2 = (GeometricObject)oc1.clone();
}
}
Don,
Please note that the access specifier for method Object::clone() is protected. It is not accessible from your TestOctagon class as you are invoking this method on object of type GeometriObject (oc1) where the clone method is still protected as neither it or its super classes have overridden it. Try moving the clone method from Octagon class to GeometriObject class. Please retain the public access specifier. Refer this sample on how to do it http://www.javatpoint.com/object-cloning
I've used validators with backing objects and annotations in Spring MVC (#Validate). It worked well.
Now I'm trying to understand exactly how it works with the Spring manual by implementing my own Validate. I am not sure as to how to "use" my validator.
My Validator:
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;
import com.myartifact.geometry.Shape;
public class ShapeValidator implements Validator {
#SuppressWarnings("rawtypes")
public boolean supports(Class clazz) {
return Shape.class.equals(clazz);
}
public void validate(Object target, Errors errors) {
ValidationUtils.rejectIfEmpty(errors, "x", "x.empty");
ValidationUtils.rejectIfEmpty(errors, "y", "y.empty");
Shape shape = (Shape) target;
if (shape.getX() < 0) {
errors.rejectValue("x", "negativevalue");
} else if (shape.getY() < 0) {
errors.rejectValue("y", "negativevalue");
}
}
}
The Shape class that I seek to validate:
public class Shape {
protected int x, y;
public Shape(int x, int y) {
this.x = x;
this.y = y;
}
public Shape() {}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}
Main method:
public class ShapeTest {
public static void main(String[] args) {
ShapeValidator sv = new ShapeValidator();
Shape shape = new Shape();
//How do I create an errors object?
sv.validate(shape, errors);
}
}
Since Errors is just an interface, I can't really instantiate it like an ordinary class. How do I actually "use" my validator to confirm that my shape is either valid or invalid?
On a side note, this shape should be invalid since it lacks an x and a y.
Why don't you use the implementation that spring offers org.springframework.validation.MapBindingResult?
You can do:
Map<String, String> map = new HashMap<String, String>();
MapBindingResult errors = new MapBindingResult(map, Shape.class.getName());
ShapeValidator sv = new ShapeValidator();
Shape shape = new Shape();
sv.validate(shape, errors);
System.out.println(errors);
This will print out all that is in the error messages.
Good luck