Question: Topics: About Java What is a compiler, and what does it do? Characteristics of the languageStrongly typed and statically typed Everything has a data type

Topics:

About Java

What is a compiler, and what does it do?

Characteristics of the languageStrongly typed and statically typed

Everything has a data type & data types must be declared

Case sensitive

Object oriented

System.out.print() vs. System.out.println()

How to use the Scanner class to obtain user input

Data typesWhat are they?

Know the basic types like: int, double, boolean, String, etc.

Variables

What is a variable?

Declarations

Initialization

Assignment

Constants

Reserved words like:

What is a reserved word?

Examples: class, if, import, static, void, while, etc.

Comments what are they and why do we use them?

Strings

String literals

Concatenation

Escape sequences

String methods:

charAt()

equals()

indexOf()

length()

substring()

toLowerCase() & toUpperCase()

Operators

Arithmetic: +, -, *, /, %

Relational: >, >=, <, <=, ==, !=

Boolean: !, &&, ||

Evaluating expressions

Arithmetic

Relational

Boolean

Decision logic structures using if and if/else statements

Be able to read simple code with if statements and evaluate the output

Be able to write simple programs of no more than 10 lines of code.

Looping structures using while, do/while, and for

Be able to read simple code with loops and evaluate the output

Be able to write simple programs of no more than 10 lines of code.

// Some example code that demonstrates many of the topics listed above

import java.util.Scanner;

public class TestCode

{

public static void main(String []args)

{

Scanner in = new Scanner(System.in);

String text = "";

int spaceCount = 0;

int wordCount = 0;

int letterCount = 0;

double averageWordLength = 0.0;

boolean inputIsValid = false;

boolean hasNoSpaces = false;

boolean startsWithSpace = false;

boolean endsWithSpace = false;

final char SPACE = ' ';

do // get a string of words from the user

{

hasNoSpaces = false;

startsWithSpace = false;

endsWithSpace = false;

System.out.print("Enter some words separated by spaces: ");

text = in.nextLine();

// if the text the user enters has no spaces

// then it cannot have more than one word.

if (text.indexOf(SPACE) == -1)

hasNoSpaces = true;

// if the text the user enters starts with a space

// then it is wrong.

if (text.substring(0, 1).equals(" "))

startsWithSpace = true;

// if the text the user enters ends with a space

// then it is wrong.

if (text.substring(text.length() - 1).equals(" "))

endsWithSpace = true;

} while (hasNoSpaces || startsWithSpace || endsWithSpace);

// count the spaces

// by looping through the character in the string index by index

for (int index = 0; index < text.length(); index++)

{

if (text.charAt(index) == SPACE)

spaceCount += 1;

}

// count the words

wordCount = spaceCount + 1; // there will be one more word than spaces

System.out.println("There are " + wordCount + " words in \"" + text + "\" ");

// find the average word length

letterCount = text.length() - spaceCount;

averageWordLength = (double)letterCount / wordCount; // average letters per word

System.out.println("There average word length is:\t\t" + averageWordLength + " ");

// get the first word and capitalize it

int indexOfFirstSpace = text.indexOf(SPACE);

String firstWord = text.substring(0, indexOfFirstSpace);

firstWord = firstWord.toUpperCase();

System.out.println("The first word in upper case is:\t" + firstWord + " ");

// get the last word and capitalize it

int indexOfLastSpace = text.lastIndexOf(SPACE);

String lastWord = text.substring(indexOfLastSpace + 1);

lastWord = lastWord.toUpperCase();

System.out.println("The last word in upper case is:\t\t" + lastWord + " ");

// which word is longer - the first word or the last word?

if (firstWord.length() > lastWord.length())

{

System.out.printf("\"%s\" is longer than \"%s\".", firstWord, lastWord);

}

else if (firstWord.length() < lastWord.length())

{

System.out.printf("\"%s\" is shorter than \"%s\".", firstWord, lastWord);

}

else

{

System.out.printf("\"%s\" and \"%s\" are the same length.",

firstWord, lastWord);

}

}

}

You will be given the following information on the exam document:

Quick Reference:

class java.lang.String

char charAt(int index) // Returns the character at the specified index.

int compareTo(String anotherString) // Compares two string lexicographically.

int length( ) //Returns the length of this string.

String substring(int from , int to ) //Returns a new string that is a substring of this string.

String substring(int from) //Returns substring(from, length())

String toLowerCase( )

int indexOf(String s) // returns the index of the first occurrence of s, returns 1 if not found

class java.util.Scanner

String next() // reads the next string token from the keyboard

String nextLine() // returns all input remaining on the current line as a string

double nextDouble()

int nextInt()

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!