I have coded and run my model in OPL, and I am trying to run code it in JAVA and run it again. As part of my Code (in OPL), I have defined a Tuple as follows:
int Y = asSet(1..7);
int k = asSet(1..42);
int G = asSet(1..2);
tuple SL {
int i;
int j;
float l;
}
{SL} SLs with i,j in Y=...; /* e.g. {<1,2,502>, <2,5,309>, <5,7,401>, <2,3,350>} */
Then, I have defined other arrays of:
int u[SLs][G]=...; /* e.g. u[<1,2,502>][1] = 50; u[<1,2,502>][2] = 83; u[<2,5,309>][1] = 75;*/
Now that I wanted to code it in Java, I have done it as follows, but I am not sure if I am right. I would appreciate if you could share your ideas.
import java.io.*;
import java.util.*;
public class Model {
public static int Y = 7;
public static int K = 42;
public static int G = 3;
public static int R = 2;
public class SL {
public int i; /* how say i is in Y*/
public int j; /* how say j is in Y*/
public int l;
List<SL> sl = new ArrayList<SL>();
Object[] SL1 = Sl.toArray();
int [][] u = new int [sL.length][G];
}
public static void Solve() {
/* How to instantiate SL1 and u[<i,j,l> in SL1][g in G] here and printout SL1:*/
}
}
and another class to run the solve() method:
public class SolverMethod {
public static void main(String[] args) {
Model.Solve();
}
}
I would appreciate if you help me out to fix and run the code.
Regards,
Bornay
You may make Map so that with tuple or object of SL you can generate unique number which can be used as a index in u var. say,
Map<SL,Integer> m;
int key=m.get(sl);
u[key][g]
and to instatiate SL1 you need to make object of SL since SL1 is not static.
SL sl=new SL();
sl.SL1 or sl.u
First create object of SL and point it's variables or methods.
Here is my implemented code below. I have made some changes.
import java.io.*;
import java.util.*;
public class Model {
public static int Y = 7;
public static int K = 42;
public static int G = 3;
public static int R = 2;
static Map<SL,Integer> m=new HashMap<>();
static List<SL> sL = new ArrayList<SL>();
static int[][] u;
static int index=0;
static public class SL {
public int i;
/* how say i is in Y*/
public int j;
/* how say j is in Y*/
public int l;
}
public static void Solve() {
/* How to instantiate SL1 and u[<i,j,l> in SL1][g in G] here and printout SL1:*/
for(int i=0;i<5;i++){
SL sl=new SL();
sl.i=i;sl.j=i+1;sl.l=i+2;
sL.add(sl);
m.put(sl, index++);
}
u=new int[m.size()][G];
for(SL s:sL){
for(int i=0;i<G;i++){
u[m.get(s)][i]=i+10;
}
}
for(SL s:sL){
for(int i=0;i<G;i++){
System.out.println(u[m.get(s)][i]);
}
}
}
public static void main(String[] arg){
Model.Solve();
}
}
Here I have made sL,m and u static because we need only single instance of it.
Related
public class Store {
// instance fields
int area;
// constructor method
public Calc(int one, int two, int three) {
area = one*two*three;
}
// main method
public static void main(String[] args) {
int sideOne = 2;
int sideTwo = 3;
int sideThree = 1;
Calc mult = new Calc(sideOne,sideTwo,sideThree);
System.out.println(mult.area);
}
}
Can anyone help a beginner understand why, when passing parameters, this is an invalid method declaration?
You define/call a Calc constructor, but there is no Calc class.
Rename your class to Calc ant your code will compile and execute correctly:
public class Calc {
// instance fields
int area;
// constructor method
public Calc(int one, int two, int three) {
area = one * two * three;
}
// main method
public static void main(String[] args) {
int sideOne = 2;
int sideTwo = 3;
int sideThree = 1;
Calc mult = new Calc(sideOne, sideTwo, sideThree);
System.out.println(mult.area);
}
}
Could some one tell me how can i get variable inside methods and the reverse.
Something like:
i want to use variable y inside that method func, and get that x from that method func and use it inside main.
class test{
int y = 4;
void func(){
int x = 3;
}
public static void main(String[] args)
{
// take x inside main
}}
You can always use class variable inside methods. To use x of func() inside main() method, you can return it from func() or save it into some class variable
class TestClass {
int y = 4;
int x = 0;
//func returning x
int func1() {
int x = y;
return x;
}
//func storing it to class variable
void func2() {
this.x = 3;
}
public static void main(String[] args) {
TestClass t = new TestClass();
int xOfFunc = t.func1();
t.func2();
System.out.println("x Of Func :: " + xOfFunc + "\n class variable x :: " + t.x);
}
}
output :
x Of Func :: 4
class variable x :: 3
class test{
int y = 4;
int func(){
int x = 3;
return x;
}
public static void main(String[] args)
{
test obj = new test();
int x = obj.func();
}
}
or you can make func() method static and you will be able to call this method without creating an object of class:
class test{
int y = 4;
static int func(){
int x = 3;
return x;
}
public static void main(String[] args)
{
int x = func();
}
}
Try something like this:
class Main {
public int y= 4;
int func(){
return 4;
}
public static void main(String... args){
Main m = new Main();
int x = m.func();
int y = m.y;
}
}
class test{
int y = 4;
int x;
void func(){
int x = 3;
this.x = 3; //make it usable from the class
}
}
y should be accessible inside the function. If the function uses a variable y by itself you can use this.y to access the variable.
Make it static like this allows you to access it everywhere by calling test.y.
class test{
public static int y = 4;
void func(){
int x = 3;
}
}
Then you can do this in main.
public static void main(String[] args)
{
int value = test.y;
}
In Java, the output of s is 0. I do not understand why and would it be possible to somehow get the correct value of s (1000 here)?
public static void main(String args) {
int s = 0;
List<Integer> list = getList(s);
System.out.println("s = " + s);
}
public static List<Integer> getList(int s) {
List<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < 1000; i++) {
list.add(i); s++;
}
}
In C# there were out descriptors to indicate that the variable is going to change if I'm not mistaken..
I'm not going to get the list.size() in general!
In Java, all method arguments are passed by value, i.e. copy. So, changes to the copy are not visible to the caller.
To address your second question, you can just use list.size() on the caller side.
I see two ways
1) Make 's' as static variable and move it to class level
2) Create class with getter/setter for list and int and return the object for getList call
public static MyWrapperObj getList(int s) {
......
return wrapperObj
}
class MyWrapperObj
{
private List<Integer>;
private countS;
....
//getter/setters.
}
Java doesn't allow for passing parameters by reference - but you could wrap it in an object like this:
class IntHolder {
private int s;
IntHolder(int s){
this.s = s;
}
public void setS(int s){
this.s = s;
}
public int getS(){
return s;
}
public void increment(){
s++;
}
}
class Test{
public static void main(String[] args) {
IntHolder s = new IntHolder(0);
List<Integer> list = getList(s);
System.out.println("s = " + s.getS());
}
public static List<Integer> getList(IntHolder s) {
List<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < 1000; i++) {
list.add(i); s.increment();
}
return list;
}
}
In java, arguments passed to methods are passed by value.. you will need to make s a global or instance variable in order to modify it in other methods. This is just the way java works. e.g.
public class Test{
private int s;
public Test(){
s=0;
increment();
//print now will be 1000.
}
private void increment(){
s = 1000;
}
}
Given the following code , from some reason it won't create an instance of MyVector . What might be the problem ? The problem occurs in the line of Main :
MyVector vec = new MyVector();
However , when I create the an instance of MyVector with the other constructor :
MyVector vec2 = new MyVector(arr);
it compile and the instance is allocated.
class Dot:
public class Dot {
private double dotValue;
public Dot(double dotValue)
{
this.dotValue = dotValue;
}
public double getDotValue()
{
return this.dotValue;
}
public void setDotValue(double newDotValue)
{
this.dotValue = newDotValue;
}
public String toString()
{
return "The Dot's value is :" + this.dotValue;
}
}
class MyVector
public class MyVector {
private Dot[] arrayDots;
MyVector()
{
int k = 2;
this.arrayDots = new Dot[k];
}
public MyVector(int k)
{
this.arrayDots = new Dot[k];
int i = 0;
while (i < k)
arrayDots[i].setDotValue(0);
}
public MyVector(double array[])
{
this.arrayDots = new Dot[array.length];
int i = 0;
while (i < array.length)
{
this.arrayDots[i] = new Dot(array[i]);
i++;
}
}
}
and Main
public class Main {
public static void main(String[] args) {
int k = 10;
double [] arr = {0,1,2,3,4,5};
System.out.println("Enter you K");
MyVector vec = new MyVector(); // that line compile ,but when debugging it crashes , why ?
MyVector vec2 = new MyVector(arr);
}
}
Regards
Ron
I copied your code into my Eclipse IDE and got an "org.eclipse.debug.core.DebugException: com.sun.jdi.ClassNotLoadedException: Type has not been loaded occurred while retrieving component type of array." Exception when I click on the arrayDots variable.
Your code is ok and working. The debugger has a problem because the Dot class is not loaded.
See also: http://www.coderanch.com/t/433238/Testing/ClassNotLoadedException-Eclipse-debugger
You could change your Main as follows (I know this is not very beautiful)
public static void main(String[] args) {
int k = 10;
double [] arr = {0,1,2,3,4,5};
System.out.println("Enter you K");
new Dot(); // the classloader loads the Dot class
MyVector vec = new MyVector(); // that line compile ,but when debugging it crashes , why ?
MyVector vec2 = new MyVector(arr);
}
Your default constructor is not visible. Add public keyword in front of the constructor.
I am developing a blackberry app in jdp plugin for eclipse.I want to store some values froma na array in the flash memory of blackberry device,& also check whether dat value already exits in the memory or not.I am giving the code which i tried to do with persistent object,bt somehw i am nt able to get want i want,plz modify the code where reqd
package com.firstBooks.series7.db;
import java.util.Random;
import com.firstBooks.series7.AppMain;
import com.firstBooks.series7.db.parser.XMLParser;
import net.rim.device.api.system.PersistentObject;
import net.rim.device.api.system.PersistentStore;
public class DBMain {
public static String answer = "";
public static String selectedAnswer = "";
public static Question curQuestion;
public static int currQuesNumber = 1;
public static int correctAnswerCount = 0;
public static int totalNumofQuestions = 50 ;
static int quesNum[] = new int[20];
static int quesNumNew[];
static int quesCount = -1;
static int randomPosition;
static PersistentObject store;
static {
store = PersistentStore.getPersistentObject( 0xf9f8c7a20bc35c51L);
}
static{
initialize();
}
private static void initialize(){
Random rgen = new Random(); // Random number generator
//--- Initialize the array
for (int i=0; i<quesNum.length; i++) {
quesNum[i] = i;
}
//--- Shuffle by exchanging each element randomly
for (int i=0; i< quesNum.length; i++) {
randomPosition = rgen.nextInt(quesNum.length);
int temp = quesNum[i];
quesNum[i] = quesNum[randomPosition];
quesNum[randomPosition] = temp;
synchronized(store) {
if(quesNum[randomPosition]!=quesNum[i]){
System.out.println("...........i can do it............ ");
store.setContents(quesNum);
store.commit();
}
}
}
}
/*Changed the code to get a unique random number
* #author: Venu
*/
public static int getQuestionNumber() {
quesCount++;
if(quesCount < quesNum.length){
synchronized(store) {
int [] quesNumNew = (int[])store.getContents();
return quesNumNew[quesCount];
}
}
else{
initialize();
quesCount = -1;
return getQuestionNumber();
}
}
}
What is the problem you are encountering? Did you try to wrap the array in an object that implements Persistable interface? It is like the Serializable interface in j2se.
also see:
http://www.blackberry.com/developers/docs/4.5.0api/net/rim/device/api/util/Persistable.html
"A class must explicitly implement this interface for the system to persistently store instances of the class."