Jump to content
Sign in to follow this  
Gandalf14141

Java Programming

Recommended Posts

I'm currently taking a Java programming class in college. I have the following 2 problems that I just can not figure out.

 

1. Given two variables number and lsd of type int, write an expression that assigns the least significant digit of number of lsd. For example, if number is 567, then lsd should get a value of 7.

 

and

 

1. Given two variables number and secondDigit of type int, write a code fragment so that the variable secondDigit gets the second digit (from right) of number. For example, if number is 56487, then secondDigit should get a value of 8. [Remark: You are allowed to write multiple Java statements. However, there is a way to achieve this by just one Java statement. ]

 

I have searched and have gotten nowhere. If someone would be able to at least guide me in the right direction I'll be able to handle the rest.

 

Thanks!

 

EDIT: I'm not asking for anyone to do it for me, I'm just asking for something so I'll be able to figure it out.

 

EDIT2: I figured out the first one.

[hide]

public class numberfive {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int number;
int lsd;

number = 567;
lsd = number%10;

System.out.println("Least Significant Digit = " + lsd);


}
}

[/hide]


Finally on here to update that I have officially quit! It's been fun.
[hide=Signature]
Gandalf14141.png
R.I.P Billy Mays and <3 My Friend C.D.S 7/8/09 <3
60,816th to 99 Fletching 03/07/09|220,309th Person to be Able to Kill Dusties 10 Year Cape on 12/20/14[/hide]

grammar2gr8dx.png

Share this post


Link to post
Share on other sites

First one:

 

lsd = number%10;

 

Second one:

 

secondDigit = (number/10)%10;

 

The second one you just divide by 10 to make the 2nd to last digit the last digit.

  • Like 1

Share this post


Link to post
Share on other sites

Ah. Thank you very much! That makes sense! I was stumped so I was hoping someone would be willing to help on here.


Finally on here to update that I have officially quit! It's been fun.
[hide=Signature]
Gandalf14141.png
R.I.P Billy Mays and <3 My Friend C.D.S 7/8/09 <3
60,816th to 99 Fletching 03/07/09|220,309th Person to be Able to Kill Dusties 10 Year Cape on 12/20/14[/hide]

grammar2gr8dx.png

Share this post


Link to post
Share on other sites

I'm currently taking a Java programming class in college. I have the following 2 problems that I just can not figure out.

 

1. Given two variables number and lsd of type int, write an expression that assigns the least significant digit of number of lsd. For example, if number is 567, then lsd should get a value of 7.

 

and

 

1. Given two variables number and secondDigit of type int, write a code fragment so that the variable secondDigit gets the second digit (from right) of number. For example, if number is 56487, then secondDigit should get a value of 8. [Remark: You are allowed to write multiple Java statements. However, there is a way to achieve this by just one Java statement. ]

 

I have searched and have gotten nowhere. If someone would be able to at least guide me in the right direction I'll be able to handle the rest.

 

Thanks!

 

EDIT: I'm not asking for anyone to do it for me, I'm just asking for something so I'll be able to figure it out.

 

EDIT2: I figured out the first one.

[hide]

public class numberfive {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int number;
int lsd;

number = 567;
lsd = number%10;

System.out.println("Least Significant Digit = " + lsd);


}
}

[/hide]

Ah. Thank you very much! That makes sense! I was stumped so I was hoping someone would be willing to help on here.

 

You could also get the length/count and extract the number that way.


Have an Android phone? Check out my Runescape apps:

https://play.google.com/store/apps/developer?id=SAA

 

 

Follow my app development Twitter here.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.