Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Write a Java program FirstLastDigitsMath.java to accept any non-negative inte

ID: 669763 • Letter: 1

Question

1. Write a Java program FirstLastDigitsMath.java to accept any non-negative integer as a command line argument, use the Integer.parseInt function to convert this to an int data type, and then compute and print the first and last digits of the number in the format (shown for the example integer 43275):

Using Math methods: Integer: 43275; First Digit: 4; Last Digit: 5

For this problem, you must use methods from the Math class, and not treat the integer as a String or use String class methods.

2. Write a Java program FirstLastDigitsString.java to accept any non-negative integer as a command line argument, but this time treat the integer as a String. Now compute the first and last digits of the number in the format (shown for the example integer 43275) using methods of the String class (not the Math class):

Using Math methods: Integer: 43275; First Digit: 4; Last Digit: 5

3. You’ll find a Java program named TestProgram.java in the folder with the assignment. This program has a main method and runs each program with test inputs. When you are confident that your programs both work, run TestProgram to test them. This program uses the following 5 non-negative integer test inputs:

Test1: 12345; Test2: 044327; Test3: 87931; Test4: 99999; Test5: 0

Your programs should compute and display the correct results for each of these test inputs, or any other non- negative integer input

Explanation / Answer

package mani;
import java.util.Scanner;

public class FirstLastDigitsString{
  
   public static void main(String[] args){
       System.out.println("Enter the number: ");
       Scanner s=new Scanner(System.in);
       String number=s.next();
      
       System.out.println("Integer: "+number);
       System.out.println("First Digit: "+number.charAt(0));
       System.out.println("Last Digit: "+number.charAt(number.length()-1));
  
   }
}

public class FirstLastDigitsMath{
   public static void main(String[] args){
       System.out.println("Enter the number: ");
       Scanner s=new Scanner(System.in);
       String number=s.next();
       int num=Integer.parseInt(number);
       System.out.println("Integer: "+num);
       int t=num;
       while (Math.abs(num) >= 10 ) {
       num= num / 10;
       }
       System.out.println("First Digit: "+Math.abs(num));
       System.out.println("Last Digit: "+(t%10));
  
   }
}

to test u can make the above programs as method in their respective classes if you want that u shout post the TestProgram.java

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote