For JAVA PLEASE!! create a class and write the following methods: a) int firstDi
ID: 3766882 • Letter: F
Question
For JAVA PLEASE!!
create a class and write the following methods:
a) int firstDigit(int n), returning the first digit of the argument
a) int lasstDigit(int n), returning the last digit of the argument
a) int digits(int n), returning the number of digits of the argument
For example, firstDigit(1729) is 1, lastDigit(1729) is 9, and digits(1729) is 4.
Provide a program that tests your methods from a different class in a non static manner.
Also if you could show me the output for each I would greatly appericate it , Thank you !!
Explanation / Answer
Digit.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author CheggExpert
*/
public class Digit {
public int firstDigit(int n)
{
int digit = 0;
while(n!=0)
{
digit = n%10;
n = n/10;
}
return digit;
}
public int lastDigit(int n)
{
return n%10;
}
public int digits(int n)
{
int count = 0;
if(n==0)
return 1;
else
{
while(n!=0)
{
n = n/10;
count++;
}
}
return count;
}
}
TestDigit.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author CheggExpert
*/
import java.util.*;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author CheggExpert
*/
;
public class TestDigit
{
public static void main(String[] args) {
// TODO code application logic here
// your code goes here
//Test your class here.
Digit digit = new Digit();
System.out.println("First digit is "+digit.firstDigit(1729));
System.out.println("Last digit is "+digit.lastDigit(1729));
System.out.println("Number of digits is "+digit.digits(1729));
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.