IST 311 Midterm Complete Solutions
IST 311 Midterm Complete Solutions Why Should You "Format" Your Code? ️•Readability -Makes it much easier to determine what the code is doing •Finding typos -Missing characters like { ; and ( can cause formatting misalignments •Conformity and Consistency -Programming teams should agree on what type of formatting Class names ️•Class names should be PascalCased (aka, MixedCase) •First character upper-case •All "words" in the class name should begin with upper-case •No underscores •They should be "singular", not "plural" •Good class names: -Person, FootballGame, Invoice, Cart, Order, CollegeCourse •Bad class names: -person, Football_Game, Invoices, collegeCourse Field & Variable Naming Conventions ️•Use camelCasing when naming fields & variables -First letter lower-case; each following word capital -No underscores or abbreviations •Good Java variable names: -netPrice, mothersMaidenName •Bad Java variable names: -NetPrice, mothers_maiden_name Constants ️•Constants should be all uppercase •With underscore (_) between words •e.g., public static final int MAX_CONNECTIONS = 5; public static final String COMPANY = "Acme"; Single-letter names: ️Don't use them (except for loop iterators and Exception) method/field/variable names ️must be meaningful Block Comments ️you can't nest block comments block comments vs line comments ️/* */ - block comments // - line comments The key to OOP (object oriented programming) ️grouping of data & functions together In OOP "data" is also called ️"fields" or "states" In OOP "functions" are also called ️"methods" or "behaviors" Four Pillars of OOP ️•Encapsulation -Surrounding & protecting fields & methods •Inheritance -Creating a new class, by way of an existing class •Polymorphism -Related-but-different objects treated similarly •Abstraction -Generalizing, simplifying -Not to be confused with "abstract" Java class, which is something different Java allows for encapsulation.... ️via data hiding (getters/setters) the keyword that Java uses to engage inheritance ️extends What is polymorphism? ️•Greek for "many forms" •Using different types of objects in the same context •They must have a common ancestor -Base class or -Interface (we'll talk about that later) •That's actually called Subtype Polymorphism -There are other kinds, but that's what most people mean for "polymorphism" example: public class Main { public static void displayVoice(Animal animal) { Sln(()); } public static void main() { Dog dog = new Dog(); Cat cat = new Cat(); displayVoice(dog); displayVoice(cat); } } Abstraction ️•Creating a representation of something, but only include characteristics relevant for its usage • When talking about classes, that means to include only fields and methods relevant to its use -Know that unlike the other 3 pillars, "abstraction" more of adesign idea, than programming A class.... ️must implement all the methods named in the interface How to recognize a concrete method and abstract method ️abstract methods do not have {} that the names of the parameters in the interface... ️do not need tomatch the implementing classes' paremeter names Interface ️Remember that a class is a blueprint for an object? •Well, an interface is a set of rules for creating a class -Sometimes compared to a real-life contract •Typically, its name begins with a capital I -ISerializeable, IDisposeable, etc... •An interface may contain abstract methods •An interface may also contain constants -e.g., public static final int MAX_COUNT = 100; int MAX_COUNT = 100; // Same as above because you have no choice in the matter •You are not able to instantiate an interface ISerializeable ser = new ISerializeable(); •You can only instantiate the class that implements that interface •A class may implement more than one interface abstract methods in an interface... ️are public that an implemented method must match... ️the signature in the interface The Point ️The interface doesn't care how you implement the class. It only cares that you have methods with the required signatures. that abstract classes can contain... ️abstract methods thatneed to be implemented that abstract classes can also... ️contain concrete methods how is the "abstract" keyword used. ️public abstract class Vehicle { private static int TANK_MAX = 12; private int tank = 0; that a class that extends an abstract class must.... ️implementall the abstract methods Limitations of Abstract Classes ️•Just like an interface, you cannot instantiate an abstract class -It can only be used as a parent class • A class can extend only one abstract class - Java does not support multiple inheritance of classes - Although you can implement multiple interfaces when to use standalone, interface, or abstract ️•Standalone class -Simple assignments/projects •Unless your teacher tells you to use interface or abstract -Only if your class is used by a very few other classes •Interface -For classes that can be implemented many different ways -You don't want to know how the methods are implemented
Document information
- Uploaded on
- March 19, 2024
- Number of pages
- 9
- Written in
- 2023/2024
- Type
- Exam (elaborations)
- Contains
- Questions & answers