Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Introduction to Programming in Java - Study Guide | CSIS 110, Study notes of Javascript programming

Material Type: Notes; Professor: Hanks; Class: Intro to Programming in Java; Subject: Computer Science Info Systems; University: Fort Lewis College; Term: Unknown 1989;

Typology: Study notes

Pre 2010

Uploaded on 08/05/2009

koofers-user-8wy-1
koofers-user-8wy-1 🇺🇸

10 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CSIS 110 - Lecture 30
Reading: Appendix E (pages 432-434)
Quiz Wednesday. Covers material up through last lecture.
Main
So far, we have only executed our java classes inside of BlueJ. What if we wanted to
share a program with someone else?
Let's run my tic-tac-toe program.
In order to do this, one of the classes in our program must contain a method with this
signature:
public static void main( String[] args )
Let's look at the parts:
- public – it's a public method, which means it can be invoked from other methods
- void – it doesn't have a return value
- String[] args – it has a formal parameter: an array of Strings
But what does static mean?
It means that this method can be called without having an instance of the class. We've
used class methods without realizing it: Math.sqrt() – I didn't create a Math object and
then call the square root method on that object. Instead, I just called the method using the
form <class name>.<method name>
Static methods are also called class methods.
main is a special static method – when we start java, it looks for a method named main
and calls it – that is what makes our program execute.
What does this mean – it means we can develop a program in java and send the class files
to someone else. That person doesn't need BlueJ or the java development kit (JDK) to run
the program (although they must have the java runtime environment (JRE) installed).
Run from a command window: java TicTacToeGame (note that .class is not part
of the command) – TicTacToeGame is the name of the class that contains the main
method.
Let's add a main method to the AddressBook project (in chapter 5):
- Create a main method in class Address book, create an Address object, and
invoke its start method
pf3
pf4

Partial preview of the text

Download Introduction to Programming in Java - Study Guide | CSIS 110 and more Study notes Javascript programming in PDF only on Docsity!

CSIS 110 - Lecture 30

Reading: Appendix E (pages 432-434) Quiz Wednesday. Covers material up through last lecture. Main So far, we have only executed our java classes inside of BlueJ. What if we wanted to share a program with someone else? Let's run my tic-tac-toe program. In order to do this, one of the classes in our program must contain a method with this signature: public static void main( String[] args ) Let's look at the parts:

  • public – it's a public method, which means it can be invoked from other methods
  • void – it doesn't have a return value
  • String[] args – it has a formal parameter: an array of Strings But what does static mean? It means that this method can be called without having an instance of the class. We've used class methods without realizing it: Math.sqrt() – I didn't create a Math object and then call the square root method on that object. Instead, I just called the method using the form . Static methods are also called class methods. main is a special static method – when we start java, it looks for a method named main and calls it – that is what makes our program execute. What does this mean – it means we can develop a program in java and send the class files to someone else. That person doesn't need BlueJ or the java development kit (JDK) to run the program (although they must have the java runtime environment (JRE) installed). Run from a command window: java TicTacToeGame (note that .class is not part of the command) – TicTacToeGame is the name of the class that contains the main method. Let's add a main method to the AddressBook project (in chapter 5):
  • Create a main method in class Address book, create an Address object, and invoke its start method
  • Create a start method o Create an Address o Add it to the AddressBook o Create another Address o Add it to the AddressBook o Lookup and print and Address We can also package all of the java class files into a single file called a Jar file. Use the Project | Create jar file function. We can then double-click the jar file to execute it. Note that this does not work well for programs that write to standard out. Now, what about String[] args. Method main has a formal parameter that is an array of Strings. But what is it used for? A: To pass values to main from the command line. public static void main( String[] args ) { System.out.println("The first command line" + " argument is: " + args[0] ); } So, what's wrong with this? What if I call the program with no arguments? What if I call it with 3? So, I'd like to change this program to
  1. Print the number of arguments
  2. Print the arguments, 1 per line Use args.length to get number of arguments. Remember, args is an array, so it has a length. Show what happens if we use double quotes around command, line arguments. Use Integer.parseInt( String s ) to extract an int from the command line arguments. What kind of method is Integer.parseInt? static toString Let's go back and look at the Student class. What happens when we try to use SOPL to display the value of a Student object? Now let's look at the TicTacToeGame class. What happens when I use SOPL to display the value of a TicTacToe object? Look at the address project – What happens when I use SOPL to display the value of an Address?

Lab 24 Create a new BlueJ project. Create a class Pet in the project. Pet's have three attributes: a name, a kind (such as "dog", "cat", or "goldfish"), and an age. Define instance variables in the Pet class for these attributes. Use an int for the age variable. Define a constructor in class Pet that has three formal parameters (give them the same names as the instance variables). The constructor initializes the instance variables with the value of the parameters. Define a toString method in class Pet. This method should return a String that contains the state of a Pet object. For example, if you have a dog named Fido that is 4 years old, toString should return something like: Fido the dog is 4 years old Now, create a second class in the project – name this class PrintPet. Create a main method in class PrintPet. The main method should:

  1. Accept three values as a command line arguments, which should be a name, a pet variety, and an age.
  2. If the number of command line arguments is not three, main should display an error message and terminate the program (You can use System.exit(1) for this).
  3. Use the value of the command line arguments to create a Pet object.
  4. Use System.out.println to display the value of the Pet object. Test your program from a command window, using the java command. For example: java PrintPet Fido dog 4 Use the homework submission system to submit your Pet and PrintPet source files. Extra Credit Modify toString so that the String is correct if the Pet is 1 year old. That is, it should create the String "Felix the cat is 1 year old" instead of "Felix the cat is 1 years old". Of course, it should still work correctly for older Pets.