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,...
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...
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
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,...
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...
Question 1: What does the following code print to the console when the main method is...
Question 1: What does the following code print to the console when the main method is run? public static void referenceMystery(int x, int[] a) { x = 3; a[0] = 4; System.out.println(x+" "+a[0]); } public static void main(String[] args) { int x = 1, y = 2; int[] a = new int[1]; int[] b = new int[1]; referenceMystery(y, b); } 1. x a[0] 2. 1 3. 2 0 4. 3 4 Question 2: What does the following code print out...
VB console .net framework I am missing something in my code below which will calculate factorials...
VB console .net framework I am missing something in my code below which will calculate factorials individually into the file to be read by a sub using theis outdated language please Not Sreamwriter or Reader fact*0, 2fact*0 Fact*1 , 2fact*1 Fact*2, 2fact*2 Fact*3, 2fact*3 (To 9) Only the first 9 (0-8) but what I have doe calculates only the 9 factorial giving a single value Public Function Calculate_fact(n_values As Integer) As Long 'calc local variables Dim fact As Long =...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT