Why is it an invalid method declaration when I am passing parameters? - java

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);
}
}

Related

Compilation error: constructor in class cannot be applied to given types

Im trying to create a subclass object using an enum from the super class but when I try to create the object in the subclass I get this error.
error: constructor Payroll in class Payroll cannot be applied to given types;
public PayClaim(int hours, PayLevel level){
^
required: PayLevel
found: no arguments
reason: actual and formal argument lists differ in length
1 error
This is my superclass Payroll
public class Payroll{
static double OVERTIME_RATE = 1.5;
static int REGULAR_WEEK = 40;
static int LEVEL_ONE_PAY = 15;
static int LEVEL_TWO_PAY = 25;
static int LEVEL_THREE_PAY = 40;
public enum PayLevel{
ONE, TWO, THREE
}
private PayLevel levels;
public Payroll(PayLevel levels){
this.levels = levels;
}
public PayLevel getPayLevel(){
return levels;
}
public static void main (String [] args) {
Payroll.OVERTIME_RATE = 1.75;
Payroll.REGULAR_WEEK = 40;
Payroll.LEVEL_ONE_PAY = 12;
System.out.println(Payroll.calculatePay(35, Payroll.PayLevel.ONE));
}
public static double calculatePay(int noHoursWorked, PayLevel level){
//do stuff
}
}
And this is my subclass PayClaim
public class PayClaim extends Payroll{
int noHoursWorked;
public Payroll.PayLevel payLevel;
double calculatedPay = 0;
public static void main (String [] args) {
PayClaim p = new PayClaim(1, Payroll.PayLevel.ONE);
System.out.println(p);
}
public PayClaim(int hours, PayLevel level){
if(hours > 80 || hours < 1){
throw new IllegalArgumentException();
}
else{
noHoursWorked = hours;
payLevel = level;
}
}
public int getNoHoursWorked(){
return noHoursWorked;
}
public PayLevel getPayLevel(){
return payLevel;
}
public double getClaculatedPay(){
return calculatedPay;
}
public void setCalculatedPay(double pay){
//
}
public String toString(){
//
}
My apologies if I missed something trivial its just that the code wont even compile so I'm really struggling to find just where I'm going wrong here.
I believe the answer you are looking for is very simple. If you invoke the parent constructor to the subclass, this should resolve the compilation problems. You can do this by using the following changes. The change I made is at the beginning of the constructor, it simply calls the parents constructor to create an object, since it is a subclass.
public class PayClaim extends Payroll{
int noHoursWorked;
public Payroll.PayLevel payLevel;
double calculatedPay = 0;
public static void main (String [] args) {
PayClaim p = new PayClaim(1, Payroll.PayLevel.ONE);
System.out.println(p);
}
public PayClaim(int hours, PayLevel level){
enter code here
super(level);
if(hours > 80 || hours < 1){
throw new IllegalArgumentException();
}
else{
noHoursWorked = hours;
payLevel = level;
}
}
public int getNoHoursWorked(){
return noHoursWorked;
}
public PayLevel getPayLevel(){
return payLevel;
}
public double getClaculatedPay(){
return calculatedPay;
}
public void setCalculatedPay(double pay){
//
}
public String toString(){
//
}

Accesor method is not recieving var from mutator method

I am making a basic calculator on eclipse, java. But I have a problem with one of the methods as it doesn't accept the right variable.
I know that the problem is in the calculateDifference() and setCurrentValue() method.
public class Dollar {
static int startingValue = 2650;
static int currentValue;
static int dollars;
static int differenceValue = calculateDifference();
static void setDollarQuantity (int dollarValue) {
dollars = dollarValue;
}
static void setCurrentValue(int currentDollar) {
currentValue = currentDollar;
}
static int calculateDifference() {
return ( currentValue - startingValue) * dollars;
}
public static void main(String[] args) {
setCurrentValue(2780);
setDollarQuantity(111);
calculateDifference();
}
}
The expected result from the calculateDifference method was 14,430 but the actual is 0. I have found the problem which was the calculateDifference method is not accepting the currentValue as 2780, but 0. Anyone can help me and modify my code?
Change
static int diffrenceValue = calculateDifference();
to
static int differenceValue;
and in main()
calculateDifference();
to
differenceValue = calculateDifference();
System.out.println(differenceValue);
This way you will set the differenceValue after the other variables are initialized with correct value, not before.

Tuple Java like tuple in OPL

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.

Java how can i get variables inside a method and the reverse

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;
}

aggregation in Java and methods in main method

I have such code which works well, but I wonder why method area is not possible to put in the main method
public class Circle {
Operation op;
double pi = 3.14;
double area(int radius) {
op = new Operation();
int rsquare = op.square(radius);
return rsquare * pi;
}
public static void main(String arg[]) {
Circle c = new Circle();
double s = c.area(5);
System.out.println(s);
}
class Operation {
int square(int n) {
return n * n;
}
}
}
Example which doesn't work:
public static void main(String arg[]) {
double area ( int radius){
op = new Operation();
int rsquare = op.square(radius);
return rsquare * pi;
}
The only way to nest method implementation code inside Java methods is with anonymous classes. In your case this would look like this (code has to be nested inside a class of sorts):
public static interface Circle {
double area(int radius);
}
public static interface Operation {
int square(int n);
}
public static void main(String arg[]) {
Circle c = new Circle() {
Operation op;
double pi = 3.14;
public double area(int radius) {
op = new Operation() {
public int square(int n) {
return n * n;
}
};
int rsquare = op.square(radius);
return rsquare * pi;
}
};
double s = c.area(5);
System.out.println(s);
}
Take
double area(){...}
Outside of the main method
Java doesn't support nested methods, even if it did, there would be no point to put a nonstatic method in a static method
The closest thing would be nested classes that contain methods, declared in the method

Categories

Resources