lecture-7 - CPSC 324 Topics in Java Programming Lecture 7...

Info icon This preview shows pages 1–6. Sign up to view the full content.

2/4/10 1 CPSC 324 Topics in Java Programming Lecture 7 Today … Go over quiz Assignment 2 notes Start on basic class inheritance Applets “lab” Reading assignments Core: Ch. 4: 144-152, 162-169 Core: Ch. 5: 171-182 CPSC 324 ‐‐ Spring 2010 2
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

2/4/10 2 Quiz 2 … Be sure to carefully read the instructions For the quiz, you were asked to find the errors , And state why these are errors But not fix the errors (which many tried to do …) CPSC 324 ‐‐ Spring 2010 3 Quiz 2 public class Ball { private static boolean inflated = true; private int diameter = 4; private void Ball(int aDiameter) { diameter = aDiameter; } public Ball(boolean inflate) { inflated = inflate; } public void inflate() { inflated = true; } public static void stretch() { diameter += 1; } public static void main(String[] args) { Ball b1 = new Ball(6); Ball b2 = new Ball(true); Ball b3 = new Ball(6, true); b2.inflate(); b2.stretch(); } } CPSC 324 ‐‐ Spring 2010 4
Image of page 2
2/4/10 3 Quiz 2 Here are the compile errors: [sbowers]$ javac Ball.java Ball.java:19: non-static variable diameter cannot be referenced from a static context diameter += 1; ^ Ball.java:23: cannot find symbol symbol : constructor Ball(int) location: class Ball Ball b1 = new Ball(6); ^ Ball.java:25: cannot find symbol symbol : constructor Ball(int,boolean) location: class Ball Ball b3 = new Ball(6, true); ^ 3 errors CPSC 324 ‐‐ Spring 2010 5 Error 1 Error 2 Error 3 Quiz 2 public class Ball { private static boolean inflated = true; private int diameter = 4; private void Ball(int aDiameter) { diameter = aDiameter; } public Ball(boolean inflate) { inflated = inflate; } public void inflate() { inflated = true; } public static void stretch() { diameter += 1; } public static void main(String[] args) { Ball b1 = new Ball(6); Ball b2 = new Ball(true); Ball b3 = new Ball(6, true); b2.inflate(); b2.stretch(); } } CPSC 324 ‐‐ Spring 2010 6 Error 1: Non-static variable Static method
Image of page 3

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

2/4/10 4 Quiz 2 public class Ball { private static boolean inflated = true; private int diameter = 4; private void Ball(int aDiameter) { diameter = aDiameter; } public Ball(boolean inflate) { inflated = inflate; } public void inflate() { inflated = true; } public static void stretch() { diameter += 1; } public static void main(String[] args) { Ball b1 = new Ball(6); Ball b2 = new Ball(true); Ball b3 = new Ball(6, true); b2.inflate(); b2.stretch(); } } CPSC 324 ‐‐ Spring 2010 7 Error 1: Non-static variable Static method Error 2: No constructor Quiz 2 public class Ball { private static boolean inflated = true; private int diameter = 4; private void Ball(int aDiameter) { diameter = aDiameter; } public Ball(boolean inflate) { inflated = inflate; } public void inflate() { inflated = true; } public static void stretch() { diameter += 1; } public static void main(String[] args) { Ball b1 = new Ball(6); Ball b2 = new Ball(true); Ball b3 = new Ball(6, true); b2.inflate(); b2.stretch(); } } CPSC 324 ‐‐ Spring 2010 8 Error 1: Non-static variable Static method Error 2: No constructor Error 3: No constructor Constructors don’t have return types (so not a constructor!)
Image of page 4
2/4/10 5 Quiz 2 public class Ball { private static boolean inflated = true; private int diameter = 4; private void Ball(int aDiameter) { diameter = aDiameter; } public Ball(boolean inflate) { inflated = inflate; } public void inflate() {
Image of page 5

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern