I'm trying to build a library with netbeans, I'm building so:
New project...Java...Java application:
package somma;
public class Somma {
public static int somma(int a, int b) {
int s = a + b;
return s;
}
}
With this main
package somma;
public class Main {
public static void main(String[] args) {
int a = 1;
int b = 2;
int s = Somma.somma(a, b);
System.out.println(s);
}
}
After that, right click on the project...properties...Buid...Packaging....click on the project again and clean and build. Now I created a Somma.jar, for to try the new library I build an exmple project:
package uselibrary;
import static somma.Somma.somma;
public class UseLibrary {
public static void main(String[] args) {
int a = 1;
int b = 2;
int s = somma(a, b);
System.out.println(s);
}
}
Run correctly, but there is a problem, when I import the library, I would like to avoid this name import static somma.Somma.somma; I would like to change with this name import somma;
How can I do that ?
Okay try this edit
package somma;
public class Somma {
public int somma(int a, int b) {
int s = a + b;
return s;
}
}
And
package uselibrary;
import somma.Somma;
public class useLibrary {
public static void main(String[] args) {
Somma som = new Somma();
int a = 1;
int b = 2;
int s = som.somma(a, b);
System.out.println(s);
}
}
Related
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.
How can I invoke a method and print its result?
public class Test {
public static int main (String args[]){
System.out.println(total);
}
public int numbers (int a, int b){
int total;
total = a + b;
return = total;
}
}
Try this instead:
public class Test {
public static void main (String args[]){
System.out.println(numbers(1, 2));
}
public static int numbers(int a, int b){
int total;
total = a + b;
return total;
}
}
Variables are scoped to the method or class in which they are defined, therefore the 'total' variable is accessible only in the 'numbers' method
public class Test {
public static void main (String args[]){
System.out.println(numbers(anumber,bnumber));
}
public static int numbers (int a, int b){
int total;
total = a + b;
return total;
}
I am learning inheritance and while doing so on Eclipse, I get an error when trying to run the following program:
import java.io.*;
import java.util.*;
public class singinh
{
void sub(int a, int b)
{
int c = a-b;
System.out.println("Diff is"+c);
}
}
public class singinh1 extends singinh {
int a,b;
void add(int a, int b)
{
this.a=a;
this.b=b;
System.out.println("Sum is"+a+b);
}
public static void main(String args[])
{
singinh1 s = new singinh1();
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
s. add(a,b);
s.sub(a,b);
}
}
The error that I get is "Error: Could not find or load main class superex$A"; What is causing this error, and how do I fix it?
As you start with java the best thing to do is to create 2 files singinh.java and singinh1.java, move the related code into the corresponding file and launch your java command using singinh1 as main class.
In singinh.java you will have:
public class singinh
{
void sub(int a, int b)
{
int c = a-b;
System.out.println("Diff is"+c);
}
}
In singinh1.java you will have:
import java.io.*;
import java.util.*;
public class singinh1 extends singinh {
int a,b;
void add(int a, int b)
{
this.a=a;
this.b=b;
System.out.println("Sum is"+a+b);
}
public static void main(String args[])
{
singinh1 s = new singinh1();
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
s. add(a,b);
s.sub(a,b);
}
}
Then you will be able to launch singinh1
In java you cannot have more than one public class in the same source file. Also the name of the source file should be the name of the public class in that source file exactly.
Since your main method is in "singinh1" class , keep it as the public class and remove public keyword from "singinh" class. Name the source file name to
singinh1.java .
Modified code :
import java.io.*;
import java.util.*;
class singinh
{
void sub(int a, int b)
{
int c = a-b;
System.out.println("Diff is"+c);
}
}
public class singinh1 extends singinh {
int a,b;
void add(int a, int b)
{
this.a=a;
this.b=b;
System.out.println("Sum is"+a+b);
}
public static void main(String args[])
{
singinh1 s = new singinh1();
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
s. add(a,b);
s.sub(a,b);
}
}
I am trying to test the class by making a program. I don't really understand how to. All I know is that it has something to do with prime numbers, but that is all I know. Can someone please help me? I would really appreciate it if you do. Thank you.
import java.util.Scanner;
public class Android
{
public static int tag = 1;
private String name;
public static int n = 0;
Android ()
{
name = "Bob";
changeTag();
}
String getName(Object input)
{
return input.getClass().getName();
}
private static boolean isPrime(int n)
{
for(int i = n - 1; i > 0; i--)
{
if (n % i == 0)
{
return false;
}
}
return true;
}
public static void changeTag()
{
do
{
tag++;
} while (!isPrime(n));
}
}
There will be a class that contains the main method. (Assume MainClass the name of the class.)
Main class looks like:
public MainClass{
public static void main(String args[]){
/*Source Code*/
}
}
And you can make Android object in main method. Like this:
Android android = new Android();
And then, JVM (Java Virtual Machine) will run the Android constructor for Android class. Depending on Android constructor, the JVM will run changeTag() method. And changeTag() method finds the Prime by increasing the tag variable.
If you want to verify the prime, use follow this code:
System.out.println("prime"+tag);
You can see that the prime(tag) is output from the console window.
Full source:
Android Class :
import java.util.Scanner; //You don't need because you don't use Scanner.
public class Android
{
public static int tag = 1;
private String name;
public static int n = 0;
Android ()
{
name = "Bob";
changeTag();
}
String getName(Object input)
{
return input.getClass().getName();
}
private static boolean isPrime(int n)
{
for(int i = n - 1; i > 0; i--)
{
if (n % i == 0)
{
return false;
}
}
return true;
}
public static void changeTag()
{
do
{
tag++;
} while (!isPrime(n));
System.out.println("prime"+tag);
}
}
Main Class :
public MainClass{
public static void main(String args[]){
Android android = new Android();
}
}
(May be an error because I didn't run this program. Explained only on the basis of the theory.)
This is some really beginner stuff. And cannot for the life of me figure out why I cannot have a method calculate two variables for me.
public class Testing {
int val1;
int val2;
int res;
public static void main(String[] args)
{
int calcResult();
}
public Testing()
{
val1 = 4;
val2 = 8;
res = 0;
}
public static int calcResult()
{
res = val1 + val2;
return res;
}
}
val1 and val2 aren't static and your method is static that's a problem.
I would recommend you to do this:
public class Testing {
private int val1;
private int val2;
private int res;
public static void main(String[] args)
{
new Testing();
}
public Testing()
{
this.val1 = 4;
this.val2 = 8;
this.res = 0;
this.calcResult();
}
public int calcResult()
{
res = this.val1 + this.val2;
return res;
}
}
static method can not access instance variables.
Remove static keyword and try something like:
public class Testing {
int val1;
int val2;
int res;
public static void main(String[] args)
{
System.out.println( new Testing().calcResult() );
}
public Testing()
{
val1 = 4;
val2 = 8;
res = 0;
}
public int calcResult()
{
res = val1 + val2;
return res;
}
}
You have multiple errors in your code. First off, you can only assign a function result to a variable, so the current code won't compile:
public static void main(String[] args)
{
int calcResult();
}
Also, you cannot reference a field (non static) variable from a static function, so also the following won't compile:
public static int calcResult()
{
res = val1 + val2;
return res;
}
because all the variables will not be available from within the static function calcResult().
Not to mention that it's generally bad practice to use field variables in calculations. What I would recommend is something like the following:
public class Testing {
public static void main(String[] args)
{
new Testing();
}
public Testing()
{
int val1 = 4;
int val2 = 8;
int res = calcResult(val1, val2);
System.out.println(res);
}
public static int calcResult(int val1, int val2)
{
return val1 + val2;
}
}