Question

In: Computer Science

1.The below code has some errors, correct the errors and post the working code. Scanner console...

1.The below code has some errors, correct the errors and post the working code.

Scanner console = new Scanner(System.in);

System.out.print("Type your name: ");

String name = console.nextString();

name = toUpperCase();

System.out.println(name + " has " + name.Length() + " letters");

Sample Ouptut:
Type your name: John
JOHN has 4 letters

   2. Write a code that it reads the user's first and last name (read in the entire line as a single string), then print the last name   followed by a comma and the first initial.

Sample output:

Type your name: John Smith

Smith, J.

Solutions

Expert Solution

Answer:

Question 1:

import java.util.*;
public class Test /* 'Test' class starts here */
{
   public static void main(String[] args) /* main() function starts here */

{
       Scanner console = new Scanner(System.in); /* 'console' object of type Scanner class is created */
       System.out.print("Type your name: "); /* displays the message on the screen */
       String name = console.nextLine(); /* nextLine() is used to read the string, and is stored in 'name' */
       name = name.toUpperCase();   /* toUpperCase() is used to convert each character of the string to uppercase */
       System.out.println(name + " has " + name.length() + " letters");
/* length() is used to find the length of the string */
   } /* End of main() */
} /* End of 'Test' class */

Output:

Question 2:

import java.util.*;
public class Test1   /* 'Test1' class starts here */
{
   public static void main(String[] args) /* main() function starts here */

{
       Scanner console = new Scanner(System.in); /* 'console' object of type Scanner class is created */
       System.out.print("Type your name: "); /* displays the message on the screen */
       String name = console.nextLine(); /* nextLine() is used to read the string, and is stored in 'name' */
       String[] tokens = name.split(" "); /* The string in 'name' is splitted at white space character and stored in 'tokens' array */
       System.out.println(tokens[1]+", "+tokens[0].charAt(0)+"."); /* tokens[1] will be 2nd word, tokens[0] is 1st word, charAt(0) is first character */
   } /* End of main() */
} /* End of Test1 */  

Output:


Related Solutions

CAN YOU CORRECT THIS CODE PLS. Scanner in = new Scanner (System.in);    // EXAMPLE 1...
CAN YOU CORRECT THIS CODE PLS. Scanner in = new Scanner (System.in);    // EXAMPLE 1 System.out.println("****************** EXAMPLE 1 *****************" ); int x = 4, y = 9; int a = 99, b = -22; if (x > y) System.out.println("x > y"); if (x < y) System.out.println("x < y"); if (x >= y) System.out.println("x >= y"); if (a <= 10023) System.out.println("a <= 10023"); if (b == y) System.out.println("b == y"); if (b == 5) System.out.println("b == 5"); if (x !=...
There are two errors in this code. Identify the errors and give the correct code that...
There are two errors in this code. Identify the errors and give the correct code that will make the program to display the following output: Rectangle: height 2.0 width 4.0 Area of the Rectangle is 8.0 ----- public interface Shape { public double getArea(); } class Rectangle implements Shape { double height; double width; public Rectangle(double height, double width) { this.height=height; this.width=width; } public double getArea() { return height*width; } public String toString() { return "Rectangle: height "+height+" width "+width;...
The following VHDL code has errors and is not working in Umhdl. please solve the errors...
The following VHDL code has errors and is not working in Umhdl. please solve the errors for the following code and rewrite the code here and show the output. The code: library IEEE; use IEEE.STD_LOGIC_1164.ALL; use IEEE.std_logic_unsigned.all; entity Car_Parking_System_VHDL is port ( clk,reset_n: in std_logic; -- clock and reset of the car parking system front_sensor, back_sensor: in std_logic; -- two sensor in front and behind the gate of the car parking system password_1, password_2: in std_logic_vector(1 downto 0); -- input...
Correct all errors in the provided script. Remember that some errors are fatal (result in an...
Correct all errors in the provided script. Remember that some errors are fatal (result in an error message) while others simply cause the program to produce incorrect results. You can assume that you have fixed all errors when the checker passes all tests. %*************************************************************************************** %It is recommended that you debug this program offline and submit only once you have corrected the errors %*************************************************************************************** %% %These 3 loops all calculate the sum of the integers from 1 through 1000 for number...
1. The following program has syntactic errors. Identify the errors and correct them. Compile the corrected...
1. The following program has syntactic errors. Identify the errors and correct them. Compile the corrected program and submit a screeshot showing that the program has 0 (zero) errors. You can use  https://repl.it/languages/c  or  https://www.onlinegdb.com/ #include <stdio.h> int main (Void) {       INT sum;       /* Compute result       sum = 25 + 37 - 19;       // Display result       printf (“The answer is %i\n”, sum);       return 0; } // main 2. What is the purpose of a variable? a. To...
Find and correct at least two errors in each of the following segments of code. a)...
Find and correct at least two errors in each of the following segments of code. a) final int ARRAY_SIZE = 5; ARRAY_SIZE = 10; int[] table = new int[ARRAY_SIZE]; for (int x = 0; x <= ARRAY_SIZE; x++) table[x] = 99; b) String[] names = {“Mina” , “George”}; int totalLength = 0; for (int i = 0; i <= names.length() ; i++) totalLength += names[i].length; c) String[] words = “Hello”,“Goodbye”; System.out.println(words.toUpperCase()); d) public class MyClass { private int x; private...
***The code is provided below*** When trying to compile the code below, I'm receiving three errors....
***The code is provided below*** When trying to compile the code below, I'm receiving three errors. Can I get some assistance on correcting the issues? I removed the code because I thought I corrected my problem. I used #define to get rid of the CRT errors, and included an int at main(). The code compiles but still does not run properly. When entering the insertion prompt for the call details, after entering the phone number, the program just continuously runs,...
The code below is giving an arithmetic overflow. Correct the below given the below code, so...
The code below is giving an arithmetic overflow. Correct the below given the below code, so that the value of s0 is printed before calling the function fun1, inside the function Fun1 and after returning from Fun1 in main. Upload the corrected .asm or .s file in the drop box. .text main: addi $s0,$zero,2 jal Disp jal Fun1 jal Disp j Exit Fun1: addi $sp,$sp,-4 sw $s0,0($sp) addi $s0,$s0,15 jal Disp lw $s0,0($sp) addi $sp,$sp,4 jr $ra Disp: li $v0,1...
C# I need working code please Write a console application that accepts the following JSON as...
C# I need working code please Write a console application that accepts the following JSON as input: {"menu": { "header": "SVG Viewer", "items": [ {"id": "Open"}, {"id": "OpenNew", "label": "Open New"}, null, {"id": "ZoomIn", "label": "Zoom In"}, {"id": "ZoomOut", "label": "Zoom Out"}, {"id": "OriginalView", "label": "Original View"}, null, {"id": "Quality"}, {"id": "Pause"}, {"id": "Mute"}, null, {"id": "Find", "label": "Find..."}, {"id": "FindAgain", "label": "Find Again"}, {"id": "Copy"}, {"id": "CopyAgain", "label": "Copy Again"}, {"id": "CopySVG", "label": "Copy SVG"}, {"id": "ViewSVG", "label": "View...
SDES C++ The code is not working currently I know the functions are correct but the...
SDES C++ The code is not working currently I know the functions are correct but the original plaintext should match the "ciphertext after" when inputting 4 #include <iostream> #include <iomanip> #include <string> #include <stdlib.h> #include <stdio.h> #include <cstdlib> #include <time.h> #include <windows.h> using namespace std; //function prototypes void splitString(string &x, string &y, string &original); void expansionFunction(string &input); string XORstring(string& str1, string& str2); string getSBOXValue(string arr[][8], string val); int binaryToDecimal(int n); string generateKey(); string findKey(string Key, int round); string Encryption(string &plaintext,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT