Download Week4

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Introduction to Programming w/ Java – Week 4
Week 4 Agenda:
This week we will review Methods – ways of placing chunks of our code into nice compartments, where each one performs
some function or procedure. For fun, we will pair up in teams of 2 people. Each person will write their own class – but only
one of these will have a main method. So, one person will write the main program, and the other person will write a class
that contains the methods that do the work. In the old days we would call this the main program and the subroutines.
Team Challenge #1 One person will write the cToFmain.java file (class), and the other person will write the cToFclass.java file (class). Notice
that the main program (cToFmain) does the following:
- declares some variables
- assigns a celcius temperature
- calls two methods
The cToFclass file has two methods:
- convertTemperature
- displayTemperature
Notice that these methods will be located in a different class file, so to use them we have to do two things:
1. Put the compiled “.class” files in the same directory.
2. Use a special command to call the method: “cToFclass.convertTemperature”, and “cToFclass.displayTemperature”
How do we do this?
You will write your programs individually, compile them individually, and then the person writing the cToFclass will email
their compiled “.class” file to the person who wrote the main method. They must save that compiled code in the same
“Week4” directory and then run the main program.
Sounds like fun, eh?
Filename: cToFmain.java
----------public class cToFmain {
public static void main (String args[]) {
double celciusTemp;
double farenheitTemp;
celciusTemp = 10.0;
farenheitTemp = cToFclass.convertTemperature(celciusTemp);
cToFclass.displayTemperature (celciusTemp, farenheitTemp);
}
}
----------Filename: cToFclass.java
----------public class cToFclass {
static double convertTemperature (double inputTemperature) {
return (9.0/5.0)*inputTemperature + 32.0;
}
static void displayTemperature (double inputCelcius, double inputFarenheit) {
System.out.println ("If Celcius = " + inputCelcius + " then Farenheit = " +
inputFarenheit);
}
}
-----------
Challenge #2 –
Now let’s get really fancy – this time pass in two parameters to your converter method – one that is the temperature and the
other that is boolean variable to indicate whether you are converting from Celcius to Farenheit or vice versa. Try to figure
this out without looking at the following code, but if you need help feel free to peek. Please save your files as new files –
remember to change your class names to match:
tempConvertClass.java
and tempConvertMain.java
Hints:
In the tempConvertMain program you’ll need to do the following:
- create a boolean variable, assign values to the temperature and the boolean variable
- call the methods to convert and display
And then, do it again, but this time:
- change the boolean variable to go the opposite direction
- call the methods to convert and display
In the tempConvertClass program you’ll need to do the following:
- use an “if/else” statement to read the boolean value in both of your methods
Filename: tempConvertMain.java
----------public class tempConvertMain {
public static void main (String args[]) {
double celciusTemp;
double farenheitTemp;
boolean CtoF;
// if CtoF is true, then we are converting Celcius to Farenheit
celciusTemp = 20.0;
CtoF = true;
farenheitTemp = tempConvertClass.convertTemperature(celciusTemp, CtoF);
tempConvertClass.displayTemperature (celciusTemp, farenheitTemp, CtoF);
farenheitTemp = 20.0;
CtoF = false;
celciusTemp = tempConvertClass.convertTemperature(farenheitTemp, CtoF);
tempConvertClass.displayTemperature (celciusTemp, farenheitTemp, CtoF);
}
}
----------Filename: tempConvertClass.java
----------public class tempConvertClass {
static double convertTemperature (double inputTemperature, boolean CtoF) {
if (CtoF == true) {
return (9.0/5.0)*inputTemperature + 32.0;
} else {
return (inputTemperature - 32.0)*5.0/9.0;
}
}
static void displayTemperature (double inputCelcius, double inputFarenheit, boolean CtoF) {
if (CtoF == true) {
System.out.println ("If Celcius = " + inputCelcius + " then Farenheit = " +
inputFarenheit);
} else {
System.out.println ("If Farenheit = " + inputFarenheit + " then Celcius = " +
inputCelcius);
}
}
}
-----------
Challenge #3 –
This time we’ll make use of the data type called an Array. An Array is basically a string of data elements (variables), like a
string of integers. To create an array of integers we would use the following code:
int[] marksArray;
marksArray = new int[10];
Note this is done in two steps. The first step declares an array of integers named “marksArray”.
The second step actually allocates computer memory for an array with 8 elements – each individual integer in the array is
called an “element”. To access the elements we use the following syntax:
marksArray[0] – this is the first element
marksArray[1] – this is the element in the 2nd position
…
marksArray[9] – this is the element in the 10th position.
In this last challenge you will create arrays of celcius and farenheit temperatures – 4 elements long. Using a for loop, we’ll
assign a value to the temperatures, convert them, and display them. Note that we will use the same tempConvertClass
methods, we will only change our main class. So let’s start by renaming it: tempConvertArrayMain.java.
Try to do this without peeking at the code – but if you get stuck look below…
Filename: tempConvertArrayMain.java
----------public class tempConvertArrayMain {
public static void main (String args[]) {
int arrayLength = 4;
int count;
double[] celciusTemp;
double[] farenheitTemp;
boolean CtoF;
// if CtoF is true, then we are converting Celcius to Farenheit
celciusTemp = new double[arrayLength]; //
farenheitTemp = new double[arrayLength];
CtoF = true;
for (count=0; count<arrayLength; count++) {
celciusTemp[count] = count*10.0;
farenheitTemp[count] = tempConvertClass.convertTemperature(celciusTemp[count], CtoF);
tempConvertClass.displayTemperature (celciusTemp[count], farenheitTemp[count], CtoF);
}
CtoF = false;
for (count=0; count<arrayLength; count++) {
farenheitTemp[count] = count*10.0;
celciusTemp[count] = tempConvertClass.convertTemperature(farenheitTemp[count], CtoF);
tempConvertClass.displayTemperature (celciusTemp[count], farenheitTemp[count], CtoF);
}
}
}-----------
Finally – in case you still want to practice using methods more, here is another example of using methods, which is also a
review from week 2 and week 3: Let’s re-write our “yanniGrades” program, placing the bulk of the work (code) in one
Method. You will notice that when we finish, the main Method only has about three lines of code (after the variables are
declared).
Program: yanniGradesMethod0.java
----------------public class yanniGradesMethod0 {
public static void main(String args[]) {
int intVariable = 1;
double doubleVariable = 60.0;
boolean booleanVariable = true;
if (booleanVariable == true) {
System.out.println("Let's Look at Yanni's Grading Scale: \n");
}
}
}
--------------------
In this first step, we set up our main Method, and then three variables – one integer, one double precision, and one boolean.
If the boolean switch is true, then we print a message. By the way, did you notice the special code which adds an extra blank
line ? – it is ‘\n’. This is called an “escape sequence” or “control code” which tells the function “System.out.println” to move
to a new line. Starting small is good – now let’s make a new Method:
Program: yanniGradesMethod1.java
----------------public class yanniGradesMethod1 {
public static void listGrades (int intParameter, double doubleParameter) {
System.out.println (“Hello, I made it into the method. intParameter and doubleParameter = “+intParameter+”,
“+doubleParameter);
}
public static void main(String args[]) {
int intVariable = 1;
double doubleVariable = 60.0;
boolean booleanVariable = true;
if (booleanVariable == true) {
System.out.println("Let's Look at Yanni's Grading Scale: \n");
listGrades(intVariable, doubleVariable);
System.out.println("Okay, we are all done, time to play Minecraft - since we understand Methods.");
}
}
}
--------------------
In this next step, we have written a Method called “listGrades”, and we have passed our two main variables in as
“parameters”. Again, this is a very important concept of programming -as by passing data into Methods it allows us to
partition our code into individual tasks. Notice that rather than writing the entire Method, we just wrote one line of test code
to ensure that our values made it in correctly. Once this is working, proceed to the next step – writing the code for the
Method listGrades. Now you can see how multiple people could write code at the same time – as one person could work on
writing the Main Method while others could work on the Methods that are called by Main.
Program: yanniGradesMethod.java
----------------public class yanniGradesMethod {
public static void listGrades (int intParameter, double doubleParameter) {
for ( ; intParameter <= 6; intParameter++) {
System.out.println("Example "+intParameter+": If Yanni scores a " + doubleParameter);
if (doubleParameter <90) {
System.out.println("Yanni's grade would be an F.\n");
} else if (doubleParameter >= 90 && doubleParameter <=100.0) {
System.out.println("Yanni's grade would be an A! Good job Yanni!\n");
} else if (doubleParameter > 100) {
System.out.println(“Yanni must be one smart dude!\n”);
}
doubleParameter = doubleParameter + 10.0;
}
}
public static void main(String args[]) {
int intVariable = 1;
double doubleVariable = 60.0;
boolean booleanVariable = true;
if (booleanVariable == true) {
System.out.println("Let's Look at Yanni's Grading Scale: \n");
listGrades(intVariable, doubleVariable);
System.out.println("Okay, we are all done, time to play Minecraft - since we understand Methods.");
}
}
}
--------------------