Lab 2 Java Primitive Types and Strings Practice Solution

$35.00 $30.80

Description

The goal of today’s lab is to become familiar with Java Data Types and key classes such as String, including their methods.

 

  1. Report on and resolve any issues with access to CS5005 Canvas. If you did not upload your lab1.txt file already, please wrap that up. Please check for feedback from one of the TAs, on Canvas-CS5005.

 

  1. Complete homework PS1. Submit the homework to Canvas for 5004as txt. Be sure to read the Oracle docs re. the remainder (%) operator. NB: You may find jshell helpful in working Problem 1. https://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.17.3 (Be sure to turn this in on for CS5004. Only the lab portions below should be submitted to CS5005.)

 

  1. Create a Class file called java, with a main method. It should declare and initialize one variable for each of the 8 primitive types described in Lecture. It should print to the console the name of the primitive type, the name of your variable, and the value assigned, one per line.

 

  1. Create a Class file called StringFun.java, with its own main Note that when using an IDE such as VSCode or IntelliJ, you will want to keep things in separate folders for each project. In particular, a given folder should only contain oneClass with a main method. Your program should demonstrate the following String operations:

 

  1. concatenation (see slide 5 from lecture)

 

  1. length

 

  1. 0-origin indexing and that blanks and periods count, using the charAt method

 

  1. substring, with either 1 or two inputs. Show that the character atthe first input is included in the substring, but the character at the second input is not included.

 

 

Page 1 of 3

 

  1. Equals andequalsIgnoreCase, demonstrating the difference

 

  1. toLowerCase andto UpperCase

 

  1. trim

 

  1. indexOf, including an example with >1 occurrence and anexample with no occurences
  2. lastindexOf, also illustrating above cases

 

  1. compareTo, showing various cases including mixed case strings

 

  1. compareToIgnoreCase

 

  1. Explain:Since Strings are immutable, why am I able to I reassign the value of a variable declared as a String?
  2. Imagine an application that requires a string with the double-quote character inside the String (perhaps to teach a Java class?). How can I create a string with a quote character in it?

 

  1. Variations on System.out.println (extension of part 2).

 

  1. Create a simple Class and main method using System.out.print as well as System.out.println

 

  1. Use System.out.printf, illustrating format specifications for at least primitive datatypes

 

  1. Create a Class Echo, to read and echo user input from its main Start with import java.util.scanner. It is sufficient for this lab to read and print strings, as well as trying out the nextLinemethod. See slides 39-45. Vary your example from what was in the lecture!

 

  1. [OPTIONAL!] Download the Code Format Sample from Piazza, illustrating proper Java documentation and other formatting. Note that the block comments that start with /** instead of just /* are special “javadoc” comments that can be used to automatically create a documentation web page for your program. From the terminal, create a folder called javadocs in the same folder as the downloaded .java code and cd into that folder. Execute from Terminal: Javadoc Calculator.java. It will generate a bunch of files ending in .html, .css, .js, and so on. Open index.html with a web browser to see where this is going! If you succeed, please capture a screen shot of the resulting .html page and upload with the rest of the lab materials. If not, no worries!

 

 

 

Page 2 of 3

 

In a future lab we will look at how to install extensions to VSCode to create javadoc output from within that editing environment and how to better direct the resulting output.

 

 

Please submit the Lab portions of this handout as Lab2 on Canvas for CS5005. Tonight if you are done, or at least before next week’s lab session. The sooner you submit, the sooner our TAs can provide helpful feedback.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Page 3 of 3