I try to learn JAVA language. I write simple program, but it don't work. Could somebody give to me advice how solve problem.
My program:
class WriteOut {
private static int sumas;
public void sud(int sds) {
sumas = sds;
}
public static void main(String arg[]) {
WriteOut sum =new WriteOut();
sum.sud(5);
System.out.println("suma: "+sum);
}
}
Output I get "suma: bandymas.WriteOut#70dea4e";
I want get answer "suma: 5"
System.out.println() calls toString() method, which comes from basic Object class.
There are two ways of fixing this code:
1) Override the standart toString() method:
class WriteOut {
private static int sumas;
#Override
public String toString(){
return String.valueOf(sumas); // returns a string with sumas value
}
public static void main(String arg[]) {
WriteOut sum =new WriteOut();
sum.sud(5);
System.out.println("suma: "+sum);
}
}
2) Or just change the System.out.println() call:
class WriteOut {
public static int sumas; // To allow System.out.println() to see this variable
public void sud(int sds) {
sumas = sds;
}
public static void main(String arg[]) {
WriteOut sum =new WriteOut();
sum.sud(5);
System.out.println("suma: "+sum.sumas);
}
}
change what's inside System.out.println() to :
System.out.println("suma: "+sum.sumas);
Exmple:
class WriteOut {
private static int sumas;
public void sud(int sds) {
sumas = sds;
}
public static void main(String arg[]) {
WriteOut sum =new WriteOut ();
sum.sud(5);
System.out.println("suma: "+sum.sumas);
}
}
Output :
suma: 5
Related
I am learning getters and setters in Java. I wrote the code below :
import java.util.*;
class test{
private int num1;
private int num2;
Scanner sc=new Scanner(System.in);
void mult(){
num1=sc.nextInt();
num2=sc.nextInt();
System.out.println("Multiply = "+(num1*num2));
}
public int getnum1(){
return num1;
}
public int getnum2(){
return num2;
}
}
class TestDemo{
void add(){
test ob=new test();
System.out.println("Num 1 = "+ob.getnum1());
System.out.println("Num 2 = "+ob.getnum2());
}
}
public class Main
{
public static void main(String[] args) {
System.out.println("Hello World");
test ob=new test();
ob.mult();
TestDemo ob2=new TestDemo();
ob2.add();
}
}
Inside the class TestDemo, I am trying to access the value of the variables num1 and num2 but in the output, I am getting 0 as shown here:
output
Can anyone help me, how can I access the data inside the num1 and num2 inside TestDemo?
You should pass existing instance of test class to TestDemo instead of creating a new one. Like,
class TestDemo{
private test ob;
//set test instance through constructor or setter
TestDemo(test ob) {
this.ob=ob;
}
void add(){
System.out.println("Num 1 = "+ob.getnum1());
System.out.println("Num 2 = "+ob.getnum2());
}
}
public class Main
{
public static void main(String[] args) {
System.out.println("Hello World");
test ob=new test();
ob.mult();
TestDemo ob2=new TestDemo(ob);
ob2.add();
}
}
public class Main {
public static void main(String[] args) {
print(10);
}
static void print(int s)
{
if (1==s) {
System.out.print(s);
}
System.out.print(s);
print(s-1);
}
}
But I want output like this:
12345678910
You existing recursion never ends. You should only make the recursive call if s >= 1.
And to print the numbers in increasing order, you need to first make the recursive call and then print the current number:
static void print(int s)
{
if (s >= 1) {
print(s-1);
System.out.print(s);
}
}
Here, try this.
public static void main(String[] args) {
print(1, 10);
}
static void print(int startValue, int endValue)
{
System.out.print(startValue);
if(startValue < endValue)
print(startValue+1, endValue);
}
Solution 1:- If you want the end result-oriented then you can try this
public static int doCalculaton(int numVal, int endVal) {
System.out.println(endVal - numVal);
if (numVal == 0) {
return numVal;
}
return doCalculaton(numVal - 1, endVal);
}
public static void main(String[] args) {
int toBeCalculateNum = 10;
doCalculaton(toBeCalculateNum, toBeCalculateNum);
}
Solution 2:- If you want to use the result after calling
public static int doCalculaton(int numVal, ArrayList<Integer> numerList) {
numerList.add(numVal);
if (numVal == 0) {
return numVal;
}
return doCalculaton(numVal - 1, numerList);
}
public static void main(String[] args) {
ArrayList<Integer> numerList = new ArrayList<>();
doCalculaton(10, numerList);
Collections.reverse(numerList);
System.out.println(numerList);
// TODO loop and do whatever you want to do
}
Actually the following program is a module of my actual program and I get expected identifier errors in this code.
class New
{
static void check(String m)
{
int start=0,end=0;
char ch[]=m.toCharArray();
for(int i=0;i<m.length();i++)
{
start=end;
if(ch[i]==32)
{
end=i;
break;
}
count(ch,start,end);
}
}
static void count(char chq[],int initial,int final)
{
//String s=new String(chq,initial,final);
System.out.println(initial+" is Initial");
System.out.println(final+" is final");
}
public static void main(String... s)
{
String k="India is a Good Nation. I Love my India";
check(k);
}
}
Probably because of you are using final as a parameter name in count method. final is a keyword in java which can not be used as a parameter name or variable name. Try -
static void count(char chq[],int initial, final int end)
{
//String s=new String(chq,initial,final);
System.out.println(initial+" is Initial");
System.out.println(end+" is final");
}
instead of
static void count(char chq[],int initial,int final)
{
//String s=new String(chq,initial,final);
System.out.println(initial+" is Initial");
System.out.println(final+" is final");
}
I am having to write a program that loads the first 10 integers' into an array. I've tried it every way and have not been able to make it work with an array. This is as far as I have gotten.
public class midtermcube2 {
public static void main(String[] args) {
int totz;
for (int numz=0; numz<=9;numz++)
{
totz=numz*numz*numz;
System.out.println(totz);
}
}
}
try this:
public class AddArray
{
public static void main(String[] args)
{
int arr[]=new int[10];
for(int i=0;i<10;i++)
{
arr[i]=i;
System.out.println(arr[i]);
}
}
}
I'm trying to make a simple pounds to kilogram converter. Not sure what I'm doing wrong because it won't print out the answer. Could someone help me out.
public class ass10 {
public static void main(String[] args) {
double lbs2kg(3);
}
public double lbs2kg(double w){
System.out.println(w/2.2);
}
}
Delete double or put a variable and also method lbs2kg() must be static
(Make it return double or a compatible type, too).
public static void main(String[] args) {
double x = lbs2kg(3);
}
use something like this:-
double x = lbs2kg(3);
You are also missing the return in your function.
Also,
public static void lbs2kg(double w){
System.out.println(w/2.2);
}
You probably want...
public class ass10 {
public static void main(String[] args) {
lbs2kg(3);
}
public static void lbs2kg(double w){
System.out.println(w/2.2);
}
}
For cleaner code, I'd do:
public class ass10 {
public static void main(String[] args) {
System.out.println(lbs2kg(3));
}
public static double lbs2kg(double w){
return w/2.2;
}
}
turns out I'm just missing static for my method.
Thanks for the help!