Assignment 7 User Defined Classes Part 2

Slides:



Advertisements
Similar presentations
Continuation of chapter 6…. Nested while loop A while loop used within another while loop is called nested while loop. Q. An illustration to generate.
Advertisements

METHOD OVERRIDING Sub class can override the methods defined by the super class. Overridden Methods in the sub classes should have same name, same signature.
1. 2 Introduction to Methods  Type of Variables  Static variables  Static & Instance Methods  The toString  equals methods  Memory Model  Parameter.
1. 2 Introduction to Methods  Method Calls  Parameters  Return Type  Method Overloading  Accessor & Mutator Methods  Student Class: Revisited.
Objects & Object-Oriented Programming (OOP) CSC 1401: Introduction to Programming with Java Week 15 – Lecture 1 Wanda M. Kunkle.
Options for User Input Options for getting information from the user –Write event-driven code Con: requires a significant amount of new code to set-up.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 10 Thinking in Objects.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 5, page 1 Sun Certified Java 1.4 Programmer Chapter 5 Notes Gary Lance
1. 2 Reference... Student stu; Reference of Student stu When the reference is created it points to a null value. Before access the reference, objects.
Interfaces. –An interface describes a set of methods: no constructors no instance variables –The interface must be implemented by some class. 646 java.
Case study Students. Array of objects Arrays can hold objects (ref to objects!) Each cell in an array of objects is null by default Sample: from student.
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
RECITATION 4. Classes public class Student { } Data Members public class Student { private String name; public String id; }
Java - Classes JPatterson. What is a class? public class _Alpha { public static void main(String [] args) { } You have been using classes all year – you.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
ICBT  Basura Ramanayaka  Eshani werapitiya  Hasitha Dananjaya.
JAVA PROGRAMMING PART III. METHOD STATEMENT Form of method statement [ ] [static] ( [ ]) { } Example public static void main(String args[])
Recitation 8 User Defined Classes Part 2. Class vs. Instance methods Compare the Math and String class methods that we have used: – Math.pow(2,3); – str.charAt(4);
Java Programming Final Keyword In Java. final keyword The final keyword in java is used to restrict the user. The final keyword can be used in many context.
Announcements Final Exam: TBD. Static Variables and Methods static means “in class” methods and variables static variable: one per class (not one per.
Constructor OverloadingtMyn1 Constructor Overloading One context in which you will regularly need to use overloading is when you write constructors for.
Classes - Intermediate
Methods.
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
Methods What is a method? Main Method the main method is where a stand alone Java program normally begins execution common compile error, trying.
COMP Inheritance and Polymorphism Yi Hong June 09, 2015.
Comp1004: Building Better Objects II Encapsulation and Constructors.
Methods. Creating your own methods Java allows you to create custom methods inside its main body. public class Test { // insert your own methods right.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Staples are our staple Building upon our solution.
Java 5 Class Anatomy. User Defined Classes To this point we’ve been using classes that have been defined in the Java standard class library. Creating.
Comp1004: Building Better Objects I Methods. Coming up Methods and Parameters – Why Parameterise? – Call by value, call by reference Return Types – Methods.
Variable scope. Variable Scope Variables do not live forever. Failing to take that into account leads to problems. Let's look at an example. Let's write.
Defining Your Own Classes II
GC211 Data structure Lecture 3 Sara Alhajjam.
Coming up Constructors Overloading With one parameter
CS 302 Week 11 Jim Williams, PhD.
CS Week 13 Jim Williams, PhD.
Class Structure 16-Nov-18.
March 29th Odds & Ends CS 239.
CSC 113 Tutorial QUIZ I.
CS1316: Representing Structure and Behavior
Chapter 10 Thinking in Objects
An Introduction to Java – Part I, language basics
Java: Getting Started Basic Class Structure
Interfaces and Constructors
CNT 4007C Project 2 Good morning, everyone. In this class, we will have a brief look at the project 2. Project 2 is basically the same with project 1.
String Methods: length substring
CS1316: Representing Structure and Behavior
Encapsulation and Constructors
Everything the light touches, Simba, will be yours
Class Structure 7-Dec-18.
More on Classes and Objects
Unit-2 Objects and Classes
S.VIGNESH Assistant Professor/CSE, SECE
Class Structure 2-Jan-19.
Classes Lecture 7 from Chapter /1/11.
Recursive GCD Demo public class Euclid {
JAVA Constructors.
CS 180 Assignment 6 Arrays.
Sampath Kumar S Assistant Professor, SECE
class PrintOnetoTen { public static void main(String args[]) {
Class Structure 25-Feb-19.
Names of variables, functions, classes
Subtype Substitution Principle
Objects with ArrayLists as Attributes
ITE “A” GROUP 2 ENCAPSULATION.
Presentation transcript:

Assignment 7 User Defined Classes Part 2

Question 1 Which is the wrong way to call the Class method public static long getNumStudents() in the Class student from the main method inside of student? A) int x = getNumStudents(); B) Student s = new Student(); int x = s.getNumStudents(); C) long x = Student.getNumStudents(); D) long x = this.getNumStudents();

Question 2 What is the problem with the following code? public class Student{ int studentID; public static getStudent(int id) { Student s = new Student(); s.studentID = id; if(id == 0) return null; else return s; } public static void main(String args[]) for(int x = 0; x < 100; x++) Student s = getStudent(x); System.out.println(s.studentID); What is the problem with the following code? A) Incorrect Return type in getStudent() B) Invalid Assignment to Student s C) Incorrect way to call getStudent() D) s is not Initialized

Question 3 When can a method in a class be called without using a class or variable name before it? When a class method is being called from an instance method When a class method is being called from another class method C) When an instance method is being called from a class method When an instance method is being called from an instance method B and D A B and D

Question 4 Which overloaded method should be removed for the following to run in java? Method 2 or 4 Method 3 and 5 Method 2 and 3 Method 5 public class Example{ void methodA(int a){} void methodA(int a, int b){} void methodA(int b, float c){} void methodA(int b, int a){} void methodA(float c, int a, int b){} }

Question 5 What is the problem with the following code? A) Incompatible variable passed to call this(id / 2) B) Can’t call a constructor after first line of another constructor C) Can’t use more than one constructor type in the class Student D) Can’t pass more than one variable to a constructor public class Student{ int studentID; String name; public Student(int id, String name) { this(id / 2); this(name); } public Student(int id) studentID = id; public Student(String name) this.name = name;