Suggested Solution

import java.util.Scanner;

public class Main{

//global variables

static String name;

static int numChars;

public static void main(String[] args){

//local variable

Scanner kbin = new Scanner(System.in;);

System.out.println("Enter your name: ");

//assign data to global variable

name = kbin.nextLine();

//call helper functions

firstLetter();

nameLength();

winLose();

System.out.println("End of program!");

}

public static void firstLetter(String[] args){

//declare local variable and assign first letter of name

String firstLetter = name.substring(0,1);

System.out.println("First letter is "+firstLetter);

}//end of helper

public static void nameLength(String[] args){

//assign data to global variable numChars

numChars = name.length();

System.out.println("Your name has "+numChars+" characters");

}//end of helper

public static void winLose(String[] args){

//local variable to store random number

int rand;

rand = (int)(Math.random()*6)+5; //random number in range 5-10

//evaluate data in global variable

if(numChars>rand){

System.out.println(name+", you win!");

}

}//end of helper

}

Task 1 Task 2 Task 3
Random Name

Task 1

A computer program processes a String of information using the following algorithm:


Follow this algorithm to design the full computer program. Use local and global variables as necessary.

Random Name

Task 2

Look at your solution to Task 1. Ask yourself:

  1. How did I choose my global variables?
  2. How did I choose my local variables?
  3. What do I still not understand about variable scope?

Using your own strategies, find answers to your questions.

Random Name

Task 3

Design a computer program that processes two numbers eg 2 bank balances.

A user is asked to enter 2 numbers into the program.

The program will show the result of adding, subtracting, multiplying and dividing the two numbers.

Using your knowledge of modular design and variable scope, design a program to solve this problem.