Advertisement
Sc2ad

Lecture Notes- 9/2/16

Sep 3rd, 2016
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.06 KB | None | 0 0
  1. Methods and classes in Java:
  2.  
  3. public class X {
  4. ...
  5. } MUST BE SAVED IN A FILE CALLED X.java
  6. the public class is always the one that the file must be named. You can only have one public class in one file at a time.
  7. If you have multiple classes in one file:
  8. public class X {
  9. ...
  10. }
  11. class Y {
  12. ...
  13. }
  14. class Z {
  15. ...
  16. }
  17. Method formats:
  18. public static void main(String[] args) {}
  19.  
  20. public: Optional statement, allows other classes to use this method, otherwise ONLY this class can use it.
  21. static: Optional statement, allows the method to be called without creating an instance of the class. (See below for examples)
  22. void: Necessary statement, the return type. Can be any type (including the basic 8 types: int, double, float, short, long, byte, char, boolean, as well as String, and other types, such as Scanner).
  23. main: Necessary statement, the name of the method. Can be any single word (By convention, use a lower case first letter to indicate it is a method).
  24. String[] args: The type of the parameter(s) that are passed to the method. 'args' is the name of the parameter in the function.
  25.  
  26. Another example of a method can be:
  27. class Calculator {
  28. static int add(int x, int y) {
  29. return x + y;
  30. }
  31. }
  32. This means that the method can be accessed without creating an instance of the class it is in, it will return an 'int' type, and it is called 'add'. It takes 2 integer parameters: x and y. It returns the sum of those two parameters. Ex: In another class I could write:
  33. System.out.println(Calculator.add(1,2));
  34. Which would print: 3
  35. In terms of static methods: System.out.println(); IS a static method. If we want to print, we only have to call that one line instead of these lines:
  36. System.out sys = new System.out();
  37. sys.println();
  38. In other words, static means we don't have to construct a class to call a method within it. This can prove to be quite useful.
  39. What I had you do at the end of the meeting:
  40. Create two classes in one file, one of them is your public class, the other is your 'child' class. If you want, another way of referring to the public and non-public classes in a file can be parent: public and child: non-public.
  41. Inside your non-public class, write two methods. One of them returns a number or a String of your choice, the other asks the user for a number or a String, and returns that to the parent class.
  42. The public class should have a main method which calls your child class, and your methods from the child class.
  43. That may seem misleading, but here is some code to help you get started:
  44.  
  45. import java.util.Scanner;
  46. public class Parent {
  47. public static void main(String[] args) {
  48. // Construct the Child class and call both methods of the child class.
  49. // Remember, if you are returning something, you need to set it to a variable, or print it!
  50. }
  51. }
  52. class Child {
  53. // EITHER RETURN A STRING OR A NUMBER HERE, DEPENDS WHAT YOU WANT
  54. public String getInput() {
  55. Scanner input = new Scanner(System.in);
  56. // Get the user input and return it
  57. }
  58. // EITHER RETURN A STRING OR A NUMBER HERE, DEPENDS WHAT YOU WANT
  59. public String getStr() {
  60. return "YO";
  61. }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement