public int getA() {
return a;
}
public void setA(int a) {
this.a = a;
}
Now if I have to increment the variable... which one is the way of doing ?
variables.setA(variables.getA()+1);
in this way a always is 1.
can i solve the problem?
That code is correct (if verbose). The following:
public class Main {
private int a = 0;
public int getA() {
return a;
}
public void setA(int a) {
this.a = a;
}
public static void main(String[] args)
{
Main variables = new Main();
System.out.println(variables.getA());
variables.setA(variables.getA()+1);
System.out.println(variables.getA());
variables.setA(variables.getA()+1);
System.out.println(variables.getA());
variables.setA(variables.getA()+1);
System.out.println(variables.getA());
}
}
prints
0
1
2
3
Given the verbosity of variables.setA(variables.getA()+1), you might want to wrap it into a method (e.g. incrementA() or addToA(int) etc)
Your way of incrementing a variable should work, but you could also define a new method:
public void incrementA(){
a++;
}
Related
Hi i am trying to solve the problem I am facing
public class exam {
public static void main(String[] args) {
test1 a = new test1();
}
int zahl(int x, int y) {
int e;
if(x>y) {
e=x-y;
}else {
e=y-x;
}
if(e==0) {
return 0;
}
int z=0;
int i=1;
while(i<=e) {
z=z+i;
i++;
}
return z;
}
}
what I want to do is to call the zahl method to the test1 class
public class test1{
private exam b;
public void init() {
b = new exam();
}
void test() {
int result = b.zahl(2, 2);
assertEquals(1, result);
}
}
this is what I have tried, but it returns nothing, even though it's supposed to show me error.
You should probably be declaring your functions with the public tag i.e. public void test() if you intend to access them from other functions outside of that package. The usual Class naming convention in Java is with capital first letter, which makes your code more readable for you and others.
For your question, I don't think you are actually invoking the test() method of the test1 class. If you want that method to get called every time, you could place it inside the default Constructor.
I'm trying to make a Calculator, so I have this Calculator.java and CalculatorTest.java using setters and getters.
public class Calculator {
private int a;
public int getA() {
return a;
}
public void setA(int calcOneA) {
this.a = calcOneA;
}
private int b;
public int getB() {
return b;
}
public void setB(int calcOneB) {
this.b = calcOneB;
}
private char sign;
public char getSign() {
return sign;
}
public void setSign(char calcOneSign) {
this.sign = calcOneSign;
}
}
import java.util.Scanner;
public class CalculatorTest {
public static void main(String[] args) {
Calculator calcOne = new Calculator();
Scanner scanner = new Scanner(System.in);
System.out.println("Введите первое число");
calcOne.setA(scanner.nextInt());
System.out.println("Введите знак математической операции");
calcOne.setSign(nextLine());
System.out.println("Введите второе число");
calcOne.setB(scanner.nextInt());
}
}
I don't know how to put math sign into calcOne.setSign(). I tried nextLine() but it says can't find symbol.
Your problem isn't the setter.
For example, you can call calcOne.setSign('a'); on its own.
Now, to reproduce that for user input, you need to fix your scanner usage, for example
calcOne.setSign(scanner.nextLine().charAt(0));
However, if you're typing something like 2 + 2, the + is called the operator, and it's not always a specific index within the string (if you type this on its own line, it might be okay)
nextLine() returns a string, so you must extract the first character:
String s = scanner.nextLine();
// check that s has one and only one character.
calcOne.setSign(s.charAt(0));
I want to get increasing value starting with 1 so that I get TK-1 all the way to TK-n.
What I've tried is:
public class Main {
addTicket();
public void addTicket() {
int a;
String ticket;
a = getA();
ticket = "TK-"+ a
System.out.println(ticket)
}
public int getA() {
int a, b;
a = 0;
b = a++;
return a;
public static void main(String[] args) {
new Main();
}
}
Sorry it's my first time learning to code, can someone explain to me why it isn't working and what should I do to make it work ?
Thanks in advance.
If you need an autoincrease ID, you need a static int:
public class Ticket {
static int ticketID = 1;
public void addTicket() {
int a;
String ticket;
a = getticketID();
ticket = "TK-" + a;
System.out.println(ticket);
}
public int getticketID () {
return ticketID++;
}
public static void main(String[] args) {
Ticket test = new Ticket();
test.addTicket();
test.addTicket();
test.addTicket();
}
}
output:
enter image description here
public class Demo {
public static void main(String[] args){
Demo instance = new Demo();
instance.init();
}
public void init() {
int size = 0;
inc(size);
System.out.println(size);
}
public int inc(int size){
size++;
return size;
}
}
When I call the code above, the number zero is returned.
Even declaring size as a class attribute instead of a local variable does not solve the problem. I understand that when a method is complete, the corresponding record (containing local variable and such) is popped off of the activation stack. But, if the size variable is declared in the init() method, and then incremented and returned in a separate method (inc()), shouldn't size be equal to 1?
When incrementing you do not assign the value to anything, it increments it, but it does not store it anywhere so the value remains 0, try doing like this.
public class Demo
{
public static void main(String[] args)
{
Demo instance = new Demo();
instance.init();
}
public void init()
{
int size = 0;
size = inc(size);
System.out.println(size);
}
public int inc(int size)
{
size++;
return size;
}
}
or like this
public class Demo
{
public static void main(String[] args)
{
Demo instance = new Demo();
instance.init();
}
public void init()
{
int size = 0;
System.out.println(inc(size));
}
public int inc(int size)
{
size++;
return size;
}
}
size = inc(size);
will solve your problem, since you are not using a public scoped variable.
If you want to make this a bit elegant (at least I think this will be a bit more handy), then you need to declare a variable as a class variable.
I will illustrate this to you:
public class Demo {
int size; //global range variable
public static void main(String[] args){
Demo instance = new Demo();
instance.init();
}
public void init() {
this.size = 0;
inc();
System.out.println(this.size);
}
public void inc(){
this.size++; //will increment your variable evertime you call it
}
}
I'm new into Java and want to improve my OOP skills.
Therefore I try to write all my "first programmes" object-oriented.
Anyway... I started a small primitives test programme:
public class Primitives {
byte b;
private void setByte (byte b)
{
this.b = b;
}
public byte getByte()
{
return b;
}
short s;
private void setShort (short s)
{
this.s = s;
}
public short getShort()
{
return s;
}
int i;
private void setInteger (int i)
{
this.i = i;
}
public int getInteger()
{
return i;
}
long l;
private void setLong (long l)
{
this.l = l;
}
public long getLong()
{
return l;
}
float f;
private void setFloat (float f)
{
this.f = f;
}
public float getfloat()
{
return f;
}
double d;
private void setDouble (double d)
{
this.d = d;
}
public double getDouble()
{
return d;
}
boolean bool;
private void setBoolean (boolean bool)
{
this.bool = bool;
}
public boolean getBoolean()
{
return bool;
}
char c;
private void setChar (char c)
{
this.c = c;
}
public char getChar()
{
return c;
}
String str;
private void setString (String str)
{
this.str = str;
}
public String getString()
{
return str;
}
public static void main(String[] args) {
Primitives prim = new Primitives();
prim.setBoolean(true);
//prim.setByte(42);
//prim.setChar("ft");
prim.setDouble(42.42);
//prim.setFloat(42);
prim.setInteger(42);
prim.setLong(424242);
//prim.setShort(0);
prim.setString("fourtytwo");
//System.out.println(integer.getInteger());
}
}
Afterward, I'm trying to call my getters in another class "Main":
public class Main {
public static void main(String[] args)
{
Primitives object = new Primitives();
int objectInt = object.getInteger();
String objectString = object.getString();
System.out.println(objectInt);
System.out.println(objectString);
}
}
My output is
0
null
How does this come?
I mean, I instanced (getInt for example) before, why I received 0?
I know there must 1000 answers here but I couldn't find any proper one.
Another question:
Why do I get "The method setByte(byte) in the type Primitives is not applicable for the arguments (int)" in my Setter? (that's why I comment some of the types out)
You should call the setter methods and set the value before you get them.
The problem with your code is : You are having two main classes and looks like you are running the one in the Main class which doesn`t initiate your variable.
The other main method inside your Primitives class never get executed, so you have not initiated your variables actually!
Each application should have one main class, and only one