lab00: Basic Java and CS 1 Tasks Basic Java and CS 1 Tasks This is individual work. Introduction Projects normally in the course will be paired programming, but you must be able to do these tasks yourself to be successful in the course. Each solution for the following tasks should be written as a separate Java program. Each indicate the language feature you are to demonstrate. The solution should be tested and the input/output dialog be copied and pasted as a comment at the bottom of each program with /* comment */ notation. This is demonstrated below in the example Java code. You may start with that sample code below to help you learn Java, but be sure to remove all the code that is irrelevant to the solution of each of these tasks. Practice good commenting from the beginning! 1. Write the classic “Hello, World” program. No need to use any object orientation, just print out a line which says: "Hello, World" in Java. 2. Input 3 numbers as double type numbers. No need to input them into an array, and no need to use any object orientation. Compute and print their sum and average. 3. Input an integer n, then in a for loop input n numbers (double), computing the sum/subtotal as each number is entered, at the end of the loop, print the average. No need to input them into an array (see #5 below), and no need to use any object orientation. Use nextDouble() in the loop. 4. Input a list of words terminated by end of input (^D or ^Z), find the longest word, print it and how long it is. Use next() and hasNext(). Also print out how many words were input. (String type, scanning, next(), end of input, while loop). 5. Revise # 3 by now saving the data in an array of doubles, allocated to hold n numbers. (basic numeric array manipulations). a. input the list of numbers into the array. b. after the array is filled, print the sum and average of the array c. find the largest number in the array and print it d. count the number of numbers larger than the average and print that count. 6. Create an array to hold up to 100 strings. a. input a few full lines of text, not just words, into the array until an empty line is entered. Use nextLine(). Count the number of lines. b. input a character (such as an 'A' or 'x'. Print all lines that start with that character. c. input a word and find and print all lines that contain that word (String manipulations, searching, and loops) Demonstration These labs aren't graded, but understanding the concepts used in the solutions are vital for sucess in the course. You should be prepared to demonstrate your working code by the end of the second week of the course. Java template As a starting point here is a basic template for a Java class definition, main program, basic control structures, array declaration, and comment for pasting in sample execution. import java.util.Scanner;
public class FirstProgram
{
/* sample Java program
Author: LKRhodes 1/5/2018
Return value: none
*/
public static void main (String [] args)
{
int n1, n2; //input values
int max; //largest number
String pos; //position of largest number
System.out.println ("Hello out there.");
System.out.println ("I will add two numbers for you.");
System.out.println ("Enter two whole numbers on a line:");
// simple set up to interactively input strings and numbers
Scanner keyboard = new Scanner (System.in);
n1 = keyboard.nextInt ();
n2 = keyboard.nextInt ();
System.out.println ("The sum of those two numbers is");
System.out.println (n1 + n2);
// simple if-then-else to find the largest and its position
if (n1>n2) {
max = n1;
pos = "first";
}else {
max = n2;
pos = "second";
}
System.out.println("The "+pos+" number is largest: "+max);
if(gcd(n1,n2)==1){
System.out.println("These numbers are relatively prime!");
}
// some simple array declaration and access
double [] array;
array = new double[max];
for (int i=0; i