Professor Kit Romano's Web Site

 



Site Index

About Me

Welcome
Definition of Program
Variable Types
Understanding Memory
Declaring Variables
Simple Commands
Concatenation and Strings
Sleep-Threads-Try Catch
Campus Scenes
Pictures
Java simplified
Application Shell Styles
Reading Data From File
Overview of Applets
Excellent Graphics Applet
Online Reference
Nested if statements
Arrays
Sorting Arrays
Using Strings
Functions
Top 20 Replies 
First program 211
Important Class Example
JAVA in SDK Environment
Understanding OOP
OOP simple example
Recursion example
Example of Inheritance
     and use of JTextArea



*** Web Related **********
Build Student Web Page ?
What is FTP
Download WS ftp95
Download Java Plug In
Applets from jbuilder to sdk
Practice Problems

 

Programming Examples

 

 

                                                



First program 211

Data and Output requirements for first 211 Program
There are 10 records and the data below is in the order
of the input commands

B M 23 210 3.45 62
W F 27 132 2.90 63
B F 31 122 1.97 58
W M 35 167 1.55 72
W M 33 235 2.89 54
B F 22 124 3.01 60
B M 25 187 3.75 65
W M 41 165 1.93 67
W F 28 145 2.72 72
W F 21 109 2.00 59

oUTPUT IS AS FOLLOWS:
1 AVERAGE WEIGHT OF PEOPLE OVER 160 LBS.
2 NUMBER OF FEMALES WITH GPA UNDER 2.00
3 NUMBER OF MALES GREATER THAN 5 FEET TALL AND UNDER 210 LBS.
4 AVERAGE GPA OF MALES
5 AVERAGE AGE OF FEMALES OVER 5 FEET WITH GPA OVER 2.10
6 TOTAL WEIGHT OF EVERYBODY
7 THE GENDER OF THE TALLEST PERSON
8 THE TOTAL AGE OF PEOPLE UNDER 170 LBS.
9 THE HIGHEST GPA OF WHITE PEOPLE
10 TOTAL BLACK MALES UNDER 30 YRS , OVER 5 FEET AND HAVE GPA > 2.88

aLSO INCLUDE AN ITEMIZED LISTING OF THE DATA (i WILL GIVE YOU
A CLUE ON THIS --- ONLY INVOLVER 3 LINES OF CODE)
RENEMBER THAT ALL AVERAGES INVOLVE 2 TOTALS.....

IMPORTANT TIP..... WHEN DOING AN AVERAGE, IF YOU DIVIDE 2 INTEGERS
YOUR RESULT WILL NOT HAVE ANY DECIMAL ACCURACY. (EVEN IF IT
IS ASSIGNED TO A FLOAT VARIABLE. tO GET AROUND THIS PROBLEM,
USE CASTING.. EXAMPLE Avg_age = (float) totalage / (float) n_people

THERE ARE 2 WAYS TO WRITE THIS PROGRAM.
1 DEAL WITH EACH OUTPUT REQUIREMENT TOTALLY SEPARATELY.
2 TRY TO ELIMINATE REDUNDANT IF STATEMENTS (HARDER)

****************************************************************************************************

 

ENTERING DATA ONE RECORD AT A TIME

 

// a good starting shell for the first real program

 

import java.awt.*;
import java.net.*;
import java.io.*; import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class Application1
{
    public static void main(String[] args)
    {
        Scanner piggy = new Scanner(System.in);
        String temp;
        int age, weight, height;
        float gpa;
        char race, gender;
        int counter=0;
        char[] test = new char[1];
        // declare the rest of your variables
        do
        {
            System.out.print("Enter race  B or W or O ? ");
            temp = piggy.nextLine();
            race = temp.charAt(0);
            System.out.print("Enter gender M or F  ? ");
            temp = piggy.nextLine();
            gender = temp.charAt(0);
            System.out.print("Enter age ? ");
            age = piggy.nextInt(); 
            System.out.print("Enter weight ? ");
            weight = piggy.nextInt();
            System.out.print("Enter GPA ? ");
            gpa = piggy.nextFloat();
            System.out.print("Enter height (inches) ? ");
            height = piggy.nextInt   ();
                piggy.nextLine();  // get to start of next line
            // make all your decisions and
            // add to appropriate counters
           counter ++;

        }

        while ( counter < 10);

        // do any averaging and print final results
        System.exit(0);

    }

}

**************************************************************************************************

// AUTOMATED VERSION

// a good starting shell for the first real program

import java.awt.*;
import java.net.*;
import java.io.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class program1_help

{
public static void main(String[] args)
{
String input ="B M 23 210 3.45 62 W F 27 132 2.90 63 ";
input = input +"B F 31 122 1.97 58 W M 35 167 1.55 72 ";
input = input +"W M 33 235 2.89 54 B F 22 124 3.01 60 ";
input = input +"B M 25 187 3.75 65 W M 41 165 1.93 67 ";;
input = input +"W F 28 145 2.72 72 W F 21 109 2.00 59";
Scanner piggy = new Scanner(input);
String temp;
int age, weight, height;
float gpa;
char race, gender;
int counter=0;
// create 3 variables for first output requirement
int total_Weight_plp_over160 = 0;
int num_plp_over160 = 0;
float avg_weight_plpover160;
// create variables for second output requirement

do
{
temp = piggy.next();
race= temp.charAt(0);
temp = piggy.next();
gender = temp.charAt(0);
age = piggy.nextInt();
weight = piggy.nextInt();
gpa = piggy.nextFloat();
height = piggy.nextInt ();
counter ++;
// logic for first output requirement
if (weight > 160)
{
total_Weight_plp_over160 = total_Weight_plp_over160 +weight;
num_plp_over160 = num_plp_over160 +1;
}
// logic for second output requirement

}
while ( counter < 10);

 


// finish calculation on first output requirement and print it
avg_weight_plpover160 = (float)total_Weight_plp_over160 / (float)num_plp_over160;
System.out.println("Average weight of people over 160 " + avg_weight_plpover160);
// finish calculation for second output requirement and print

System.exit(0);

}
}