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

Java Fundamentals Lab Objectives Use string methods to manipulate string data Co

ID: 3588442 • Letter: J

Question

Java Fundamentals

Lab Objectives

Use string methods to manipulate string data

Communicate with the user by using the Scanner class or dialog boxes

Introduction

This lab introduces communicating with the user by investigating the lines of code that we need to add in order to use the Scanner class. We will also learn the method call needed for output.

The String class is introduced and we will use some of the available methods to prepare you for string processing.  
  

Task #1 Using the Scanner Class for User Input

Download the file Lab2b.zip from Blackboard. Extract it, and open the project folder in Netbeans.

Add an import statement above the class declaration to make the Scanner class available to your program.

In the main method, create a Scanner object and connect it to the System.in object.

Declare a String variable for firstName, lastName, and fullName. Declare a char variable for firstInitial.   

Prompt the user to enter his/her first name.

Read the name from the keyboard using the nextLine method, and store it into a variable called firstName (you will need to declare any variables you use).

Prompt the user to enter his/her last name.

Read the name from the keyboard and store it in a variable called lastName.

Concatenate the firstName and lastName with a space between them and store the result in a variable called fullName, using the “+” sign.

Print out the fullName.

Compile, debug, and run, using your name as test data.

Hint:
           import java.util.Scanner;
           -----
          Scanner keyboard = new Scanner(System.in);
         String varA = keyboard.nextLine( );

Task #2 Working with Strings

  

1. Use the charAt method to get the first character in firstName and store it in a variable called firstInitial

Example

String name = “Mike”;

char letter = name.charAt(0);

Print out the user’s first initial.

Use the toUpperCase method to change the fullName to all capitals and store it back into the fullName variable

Example

      String name = “Mike”;
      name = name.toUpperCase();

Add a line that prints out the value of fullName and how many characters (including the space) are in the string stored in fullName (use the method length to obtain that information).   Here’s an example of how to use the .length method:

int len = variable.length( );

Compile, debug, and run. The new output should have your initials and your full name in all capital letters, and the length of your full name.  

This is what I have so far:

package lab2b;
import java.util.Scanner;

/**
*
* @author Charters
*/

//LAB 2B
//TASK #1 Add import statement here to use the Scanner class
//TASK #1 (Alternate) Add import statment to use JOptionPane class

public class Lab2b
{

public static void main(String[] args)
{
  
  
String firstname;
String lastname;
  
Scanner keyboard = new Scanner (System.in);
  
System.out.println("What is your first name?");
firstname = keyboard.nextLine();

System.out.println("what is your last name?");
lastname = keyboard.nextLine();
  
char start = firstname.charAt(0);
System.out.println(start);
  
fullname = firstname + " " + lastname;
  
System.out.println("Your fullname is " + fullname.toUpperCase());
  
//   
int len = fullname.length();
System.out.println("The length of " + fullname + " is : " + len);
//declare additional variables here(i.e. firstName)

//TASK #1 Create a Scanner object here (not used for alternate)

// ADD LINES FOR TASK #1 HERE
// prompt the user for first name

// read the user's first name

// prompt the user for last name

// read the user's last name

// concatenate the user's first and last names

// print out the user's full name

System.out.println(); // to leave a blank line

// ADD LINES FOR TASK #2 HERE
// get the first character from the user's first name

// print out the user's first initial

// convert the user's full name to all capital letters

// print out the user's full name in all capital letters

// and number of characters in it
System.out.println(); // to leave a blank line

}
}

I know I'm missing something. Need help finishing this.

Explanation / Answer

package lab2b;
import java.util.Scanner;

/**
*
* @author Charters
*/

//LAB 2B
//TASK #1 Add import statement here to use the Scanner class
//TASK #1 (Alternate) Add import statment to use JOptionPane class

public class Lab2b
{

    public static void main(String[] args)
    {
     

       //declare additional variables here(i.e. firstName,lastname,fullname)
        String firstname;
        String lastname;
        String fullname;
     
        //TASK #1 Create a Scanner object here (not used for alternate)

        Scanner keyboard = new Scanner (System.in);

        // ADD LINES FOR TASK #1 HERE
        // prompt the user for first name

        System.out.println("What is your first name?");

        // read the user's first name
        firstname = keyboard.nextLine();
     // prompt the user for last name
        System.out.println("what is your last name?");
     // read the user's last name
        lastname = keyboard.nextLine();

        // concatenate the user's first and last names

        fullname = firstname + " " + lastname;
   
        // print out the user's full name
        System.out.println("Your fullname is : " + fullname);
             
        System.out.println(); // to leave a blank line

        // ADD LINES FOR TASK #2 HERE
        // get the first character from the user's first name

        char start = firstname.charAt(0);
     // print out the user's first initial
        System.out.println("Your First Intial : "+start);
      
      

        // convert the user's full name to all capital letters
// again converted uppercase fullname storing in fullname.
      
        fullname=fullname.toUpperCase();
      
        // print out the user's full name in all capital letters
        System.out.println("Your fullname in Capital's is : " + fullname);
      
        // and number of characters in it
        int len = fullname.length();
        System.out.println("The length of " + fullname + " is : " + len);
      
        System.out.println(); // to leave a blank line

    }
}

/*output:-

//task1

What is your first name?
james
what is your last name?
bond
Your fullname is : james bond

//task2

Your First Intial : j
Your fullname in Capital's is : JAMES BOND
The length of JAMES BOND is : 10


*/

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