suppose i have the following class
public class foo{
String a;
String b;
String c;
String d;
... // more Variables
public String getA(){
return a;
}
public void setA(String a){
this.a = a;
}
... // more getters and setters
}
what is the easiest way in eclipse to generate the following constructor
public foo (String a, String b, String c, ...){
this.a = a;
this.b = b;
this.c = c;
...
}
Source > Generate Constructor using Fields... (Alt+Shift+S, O)
Create a model class complete with getters/setters and constructors in 35 seconds using the 'Source' menu (with shortcuts!). #EclipseTips
Related
In sweep line algorithms, I use an array which sorts points by their x-coordinates, and a TreeSet that sorts points by their y-coordinates. Currently, I used two point classes to indicate which comparator to be used (i.e. whether to compare the x-coordinate or the y-coordinate). Is this possible with one point class but two compareTo functions? To illustrate, here's what I have. Apologies for the unprofessional terminology.
public static class Point implements Comparable<Point>{
int x; int y;
public Point(int a, int b){
this.x=a; this.y=b;
}
public int compareTo(Point other){
if(this.x<other.x)return -1;
if(this.x>other.x)return 1;
if(this.y>other.y)return 1;
return -1;
}
}
public static class Point2 implements Comparable<Point2>{
int x; int y;
public Point2(int a, int b){
this.x=a; this.y=b;
}
public int compareTo(Point2 other){
if(this.y<other.y)return -1;
if(this.y>other.y)return 1;
if(this.x>other.x)return 1;
return -1;
}
}
Both arrays and TreeSets allow sorting based on custom Comparators:
new TreeSet<>(new Comparator<T>() {
#Override
public int compare(T t1, T t2) {
// your implementation here
}
});
Arrays.sort(arr, new Comparator<T>() {
#Override
public int compare(T t1, T t2) {
// your implementation here
}
});
I know this is a basic question but I really struggle with this :(
First class:
public class A{
C c= new C();
B b= new B();
public static void main(String[] args) {
b.start();
System.out.println(c.getSomething());
}
}
Second Class:
public class B{
C c= new C();
public void start(){
c.setSomething(2);
}
}
Third Class:
public class C{
int x;
public int getSomething() {
return x;
}
public void setSomething(int x) {
this.x = x;
}
}
Now I know I make a new object in class A thats why the sysout returns null.
How can I make it so that I GET the value 2 instead of null in class A, and that I'm able to SET things in class B.
So I stay at the same object so to say.
I just want to be able to SET things in class B and GET that same value from the setter-getter-class-C, in class A. Please help
Thanks in advance, Jimme
I want simulate a functional programming Option class with map and flatMap but It can't work in Unity engine. as a example, I create three .cs file as follow:
1 .NET 2.0 default delegate is't covariance and contravariance, so I override it.
using System;
namespace Functional
{
public delegate R Func<out R>();
public delegate R Func<in A, out R>(A a);
public delegate R Func<in A, in B, out R>(A a, B b);
public delegate R Func<in A, in B, in C, out R>(A a, B b, C c);
public delegate R Func<in A, in B, in C, in D, out R>(A a, B b, C c, D d);
public delegate R Func<in A, in B, in C, in D, in E, out R>(A a, B b, C c, D d, E e);
public delegate void Action<in A>(A a);
}
2 define Option classes
using UnityEngine;
using System.Collections;
using Functional;
namespace Lorance.RxScoket.Util {
public interface IOption<out T> {
void Foreach (Action<T> func);
IOption<R> Map<R> (Func<T, R> func);
IOption<R> FlatMap<R> (Func<T, IOption<R>> func);
bool IsEmpty();
T Get ();
}
public static class IOptionEx {
public static T GetOrElse<T> (this IOption<T> opt, Func<T> defaultValue) {
return opt.IsEmpty() ? defaultValue() : opt.Get();
}
}
public abstract class Option<T>: IOption<T> {
public abstract bool IsEmpty ();
public abstract T Get ();
public void Foreach (Action<T> act) {
if (!IsEmpty ()) {
act (Get());
}
}
public IOption<R> Map<R>(Func<T, R> func) {
if (IsEmpty ())
return new None<R> ();
else
return new Some<R>(func (Get()));
}
public IOption<R> FlatMap<R>(Func<T, IOption<R>> func) {
if (IsEmpty ())
return new None<R> ();
else
return func (Get());
}
public static Option<T> apply(T value){
if (value == null)
return new None<T> ();
else
return new Some<T> (value);
}
public static Option<T> Empty = new None<T>();
}
public class Some<T> : Option<T> {
T value;
public Some(T value){
this.value = value;
}
public override bool IsEmpty() {return false;}
public override T Get() {
return value;
}
}
public class None<T> : Option<T> {
public override bool IsEmpty() {return true;}
public override T Get() {
throw new NoSuchElementException ("None.Get()");
}
}
public class NoSuchElementException : System.SystemException {
public NoSuchElementException(string message)
: base(message)
{
}
}
}
3 crash example
using UnityEngine;
using System.Collections;
using Lorance.RxScoket.Util;
public class TestFlatMap : MonoBehaviour {
// Use this for initialization
void Start () {
Dog d = new Dog ();
IOption<Dog> dogOpt = new Some<Dog> (d);
//ok
Dog dog = dogOpt.GetOrElse<Dog> (() => new Dog());
//crash at this point. does it care about polymorphism? but it works on pure .net project
Animal animal = dogOpt.GetOrElse<Animal> (() => new Cat());
print ("r2 -" + animal);
}
}
public class Animal {}
public class Dog: Animal {}
public class Cat: Animal {}
Does someone also meets the problem and how to solve it?
a complete crash example at : https://github.com/Unlimited-Works/crash-eg
Get the crash with this step:
1. Open the project with unity editor.
2. Open scene 'eg.unity'.
3. Press play.
4. result: Unity crashes.
it crashes when executing 'TestFlapMap.cs':
Animal animal = dogOpt.GetOrElse (() => new Cat());
if you also encounter the promblem please give me a vote at unity issues, it has delay one month ago and not any response from Unity official.
thanks
I just want to know if i have a code like -
public class A {
static String b = "hello";
public static String a() {
String b = b();
String d = d(b);
String e = e(d, b);
return e;
}
public static String b() {
return b;
}
public static void c(String c) {
b = c;
}
public static String d(String d) {
return d;
}
public static String e(String e1, String e2) {
return e1 + e2;
}
}
than how will i write the test cases for this using power mockito since all the methods are static and a method is even void too.
The first thing i got is that i only need to write the test case for method a since it is using all the other methods so all others will also get cover in it.
Can anybody help me and tell me how i can test this .
Basically, I'm trying to call a method in another class, but I'm running into errors.
Here is the code:
public class Ocean
{
private static int gridWidth, gridHeight;
private int safeCount;
private boolean shipSafe;
private static Ship[] ships;
private static int[][] grids;
public Ocean(int a, int b, int c)
{
grids = new int[a][b];
ships = new Ship[c];
for(int i = 0; i < c; i++)
{
ships[i] = Ship();
}
gridWidth = a;
gridHeight = b;
}
I cannot access the public Ship class, and the error appears in ships[i] = Ship();
I've tried redefining it, adding a constructor.
change the line ships[i] = new Ship();