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

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;...
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,...
3. Given the test code below, what is the output to the console? public class TestMe{...
3. Given the test code below, what is the output to the console? public class TestMe{ public TestMe(){     System.out.print(“a”); } public void setUp(){     System.out.print(“b”); } public void tearDown(){     System.out.print(“c”); } @Test public void testX(){     System.out.print(“x”); } @Test public void testY(){     System.out.print(“y”); } } A. abxcbyc B. abxcabyc C. bxcbyc D. abxyc
The following code segment which uses pointers may contain logic and syntax errors. Find and correct...
The following code segment which uses pointers may contain logic and syntax errors. Find and correct them. a) Returns the sum of all the elements in summands int sum(int* values) { int sum = 0; for (int i = 0; i < sizeof(values); i++) sum += *(values + i); return sum; } b) Overwrites an input string src with "61C is awesome!" if there's room. Does nothing if there is not. Assume that length correctly represents the length of src....
Develop the following code for quiz (i am getting some errors)in python in such a manner...
Develop the following code for quiz (i am getting some errors)in python in such a manner that it shoulde be having extra 3 attempts(for wrong answrs) for all questions if the user entered wrong answer · for ex: If the user entered correct answer for first question #then 3 attempts will be carried to next questions. If the user entered 3 times wrong answer in 1st question itself means it sholud display as no more attempts and you got o...
Write the code in Java: 1. Create a method that displays your name in the console....
Write the code in Java: 1. Create a method that displays your name in the console. This method is void and takes no parameters. Make an app that runs the method in response to a button press. 2. Create a version of the method in #1 that takes the text (String) to be displayed as a parameter. Allow the user to enter the text in a dialog box or text field and display that text in the console. Be sure...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT