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!
Related
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
}
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 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
I am trying to call my object via the main function. Since it needs static reference , but I somehow can't get to do it. Can someone please tell me what am I doing wrong?
private double checking;
private double saving;
public BankDisplay(double checking,double saving) // Constructor for subclass
{
checking=1000;
saving=1000;
}
public void setChecking(double checking){
this.checking=checking;
}
public double getChecking(){
return checking;
}
public void setSaving(double saving){
this.saving= saving;
}
public double getSaving(){
return saving;
}
BankDisplay checking1=new BankDisplay(checking, saving);
BankDisplay savings1= new BankDisplay(checking,saving);
When I am trying to print the object checking1 and saving1 in main it is showing "cant have a non static reference for a static function".
public BankDisplay(double checking,double saving) // Constructor for subclass
{
this.checking=checking;
this.saving=saving;
}
There is an error in constructor.
public static void main(String[] args){
BankingDisplay d1 = new BankingDisplay(100.15,200.15);
System.out.println(d1.getChecking());
}
Your constructor's method is wrong, it should be:
public BankDisplay(double checking,double saving) // Constructor for subclass
{
this.checking = checking;
this.saving = saving;
}
You also should have a toString() function in your class for you to appropriately print objects.
Such as:
public String toString() {
return String.format("Checking: %s\nSavings: %s\n", this.checking, this.saving);
}
Use like this:
System.out.println(checking1.toString());
public class BankDisplay {
private double checking;
private double saving;
public BankDisplay(double checking,double saving) // Constructor for subclass
{
this.checking= checking;
this.saving= saving;
}
public void setChecking(double checking){
this.checking=checking;
}
public double getChecking(){
return checking;
}
public void setSaving(double saving){
this.saving= saving;
}
public double getSaving(){
return saving;
}
}
public class Main {
public static void main(String[]args){
BankDisplay account1 = new BankDisplay(1000,100);
System.out.println(account1.getChecking());
}
}
This should fix your issue :D you needed this in your constructor because you were setting the constructors values, not your private variables.
Pretty new here in java. I'm trying to add methods to my otherwise currently working program to print out amount of characters from an args array. Trying to install a new method, I'm having trouble with calling args. This is my current code, and the red outline is.
static void amountOfCharsInSentence() {
int sum=0;
for (String s: args) { //args on this line is marked red
sum+=s.length();
}
}
public static void main(String[] args) {
amountOfCharsInSentence();
}
Any hint or tips in the right direction would be appreciated.
args is an argument to main, which means you can only use that argument (which is like a local variable) within main.
If you want to use it elsewhere, you have two options. The most suitable is to pass it to your other function as an argument:
static void amountOfCharsInSentence(String[] args) {
// Declare argument here -----------^^^^^^^^^^^^^
int sum=0;
for (String s: args) { // `args` here is the argument to *this* function
sum+=s.length();
}
}
public static void main(String[] args) {
amountOfCharsInSentence(args);
// Pass it here --------^^^^
}
Your other option is to create a static member of your class, which all methods in your class have access to, and assign args to that static member:
class YourClass {
static String[] commandLineArgs;
static void amountOfCharsInSentence() {
int sum=0;
for (String s: commandLineArgs) { // Use the static member here
sum+=s.length();
}
}
public static void main(String[] args) {
commandLineArgs = args; // Assign the static member here
amountOfCharsInSentence();
}
}
...but that's probably not appropriate in this case.
You have not passed the args.
static void amountOfCharsInSentence(String[] args) {
int sum=0;
for (String s: args) { //now it will not give red line.
// red line is indicated because there is no args inside this method
sum+=s.length();
}
}
public static void main(String[] args) {
amountOfCharsInSentence(args);
}
Hope this solves your problem