CIS 260 - Problem Solving Examples – Chap 6 & 7 – March 8.

package cis260.demo;

import java.io.*;

//------//

// Class NumWords //

// Computes the number of words specified by an input file //

//------//

public class NumWords

{

public static void main(String[] args) throws IOException

{

String lineOfText; // A single line of text

int wordCount=0; // The total number of words in the input file

// Declare I/O streams

Scanner inFile= new Scanner(new FileReader("c:\\input.txt"));

// Makes the output look nicer

System.out.println();

while (inFile.hasNext() )

{

lineOfText = inFile.nextLine();

// Echo print the data

System.out.println(lineOfText);

// Run through each character of the line just read in

for (int i=0;i<(lineOfText.length());i++)

{

// we are at the end of the line

if (i == lineOfText.length()-1)

{

// Check for trailing spaces...otherwise its a word

if ( !((lineOfText.substring(i,i+1)).equals(" ")))

wordCount++;

}

// We are somewhere in the middle of the line

else

{

// Look for a non-space followed by a space. This is a word

if (!((lineOfText.substring(i,i+1)).equals(" ")) &

((lineOfText.substring(i+1,i+2)).equals(" ")))

wordCount = wordCount + 1;

}

}

// Get the next line

lineOfText = inFile.nextLine();

}

// Display the results

System.out.println();

System.out.println();

System.out.println("There are " + wordCount + " words in the" +

" above document.");

}

}

package cis260.demo;

import java.io.*;

//------//

// Class SumOfSquares //

// Computes the sum of squares from one to a specified integer //

//------//

public class SumOfSquares

{

public static void main(String[] args) throws IOException

{

int count; // The number of squares to sum

double result=0; // The final result of the addition and squares

// Initialize input stream

Scanner in = new Scanner (System.in);

// Prompt the user and store the result in count

System.out.println("Enter the number of sum of square terms, negative" +

" number to stop.");

count = Integer.parseInt(in.nextLine());

// Will continue to execute loop until user specifies a negative number

while (count > 0)

{

// Perform the computations

for (int i=1;i<=count;i++)

{

result = result+Math.pow(i,2);

}

// Print out the results

System.out.println("The sum of the first " + count + " squares is " +

result + ".");

// Reset and Re-prompt

result = 0;

System.out.println("Enter the number of sum of square terms, -1 " +

"to stop.");

count = Integer.parseInt(in.nextLine());

}

}

}

package cis260.demo;

import java.io.*;

//------//

// Class Diamond //

// Draws a diamond of specified width to an output file //

//------//

public class Diamond

{

public static void main(String[] args) throws IOException

{

int width; // Width of the diamond

int beginning; // Where to start drawing '*'

int end; // Where to stop drawing '*'

// Initialize I/O streams

Scanner in = new Scanner(System.in);

Scanner out = new Scanner(new PrintWriter("c:\\outfile.txt"));

// Prompt user for diamond width

System.out.println("Enter the width of the diamond and press Enter.");

width = Integer.parseInt(in.nextLine());

// Make sure width is an odd number

if ( width%2 == 0 )

width++;

// Initialize beginning and end locators

beginning = width/2+1;

end = beginning;

// Print each row

for (int i=0;i<width;i++)

{

// Print each column

for (int j=1;j<=end;j++)

{

if (j<beginning)

out.print(" ");

else

out.print("*");

}

// New line

out.println("");

// Change beginning and end necessary to draw top half of diamond

if (i < width/2)

{

beginning--;

end++;

}

// Change beginning and end necessary to draw bottom half of diamond

else

{

beginning++;

end--;

}

}

// Finish up

out.close();

}

}

Just a fragment !

//------//

// String ConvertAbbrevToName(String) //

// Converts the abbreviation given in the first two characters of the //

// input string into a state name and returns the name of the state //

//------//

public String ConvertAbbrevToName(String abbreviation)

throws AbbreviationException

{

switch (abbreviation.charAt(0))

{

case 'A':

switch (abbreviation.charAt(1))

{

case 'L': return "Alabama";

case 'K': return "Alaska";

case 'Z': return "Arizona";

case 'R': return "Arkansas";

default: throw new AbbreviationException(

"No state with this abbreviation");

}

case 'C':

switch (abbreviation.charAt(1))

{

case 'A': return "California";

case 'O': return "Colorado";

case 'T': return "Connecticut";

default: throw new AbbreviationException(

"No state with this abbreviation");

}

case 'D':

switch (abbreviation.charAt(1))

{

case 'E': return "Delaware";

default: throw new AbbreviationException(

"No state with this abbreviation");

}

default : throw new AbbreviationException(

"No state with this abbreviation");

}