Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Object-Oriented Programming Applications solve and program the frst and seconde

ID: 666232 • Letter: O

Question

Object-Oriented Programming Applications

solve and program the frst and seconde parts only in java

each part in seprate code

using bluej

give the steps how to run it

see the link

http://www.johnloomis.org/ece538/asgn/asgn10/ece538_10.html

this is the sorce

http://www.johnloomis.org/ece538/

1. Extent your circuit analysis program to handle multiple voltages sources, such as the example below, and calculate the unknown node values. Display your results in a TextArea (see TextAreas in Jama) Format your node variables to distinguish voltages from currents. In the example, there are four nodes and two voltage sources, so the matrix is 6 x 6 and the node values would be I (2) The LTSpice version of this circuit can be downloaded from test6 zip R1 1 2 100 R2 3 4 400 R3 2 0 500 R4 4 0 200 VA 1 0 S VB 3 25 V2 R1 R2 V1 100 400 R3 R4 500 200 op 2. Modify your Java program from asgn 9 (Q2) so that you can also select and draw wires. The netlist description of a wire would be WIRE xl yl xn yn 3. Modify your earilier image display programs so that you can use a to do the equivalent of imcrop in MATLAB. Your program should write out the cropped image to an output image file. 4. Modify the robot st by adding a controller that allows the user to change the velocity of the robot only by calling the setVelocity method. The goal is to steer the robot around the screen. 5. Show the family trees for JFrame, JPanel, JLabel, and JButton.

Explanation / Answer

Object-oriented programming, or OOP:

      OOP is an approach to problem solving where all computations are carried out using objects. An object is a component of a program that knows how to perform certain actions and how to interact with other elements of the program. Objects are the basic units of object-oriented programming. A simple example of an object would be a person. Logically, you would expect a person to have a name. This would be considered a property of the person. You could also expect a person to be able to do something, such as walking or driving. This would be considered a method of the person.

Code in object-oriented programming is organized around objects. Once you have your objects, they can interact with each other to make something happen. Let's say you want to have a program where a person gets into a car and drives it from A to B. You would start by describing the objects, such as a person and car. That includes methods: a person knows how to drive a car and a car knows what it is like to be driven. Once you have your objects, you bring them together so the person can get into the car and drive.

Classes

A class is a structure that defines the data and the methods to work on that data. When you write programs in the Java language, all program data is wrapped in a class, whether it is a class you write or a class you use from the Java platform API libraries.

The ExampleProgram class from the simple program in the first lesson of Part 1 is a programmer-written class that uses the java.lang.System class from the Java platform API libraries to print a character string to the command line.

Classes in the Java platform API libraries define a set of objects that share a common structure and behavior. The java.lang.System class used in the example defines such things as standard input, output, and error streams, and access to system properties. In contrast, the java.lang.String class defines character strings.

In the example, you do not see an explicit use of the String class, but in the Java language, a character string can be used anywhere a method expects to receive a String object. During execution, the Java platform creates a String object from the character string passed to the System.out.println call, but your program cannot call any of the String class methods because it did not instantiate the String object.

If you want access to the String methods, you can rewrite the example program to create a String object as follows. This way, you can call a method such as the String.concat method that adds text to the original string.

Output

Objects

An instance is an executable copy of a class. Another name for instance is object. There can be any number of objects of a given class in memory at any one time.

In the last example, four different String objects are created for the concatenation operation, text object, text2 object, and a String object created behind the scenes from the " that uses classes and objects" character string passed to the String.concat method.

Also, because String objects cannot be edited, the java.lang.String.concat method converts the String objects to StringBuffer (editable) string objects to do the concatenation.

Besides the String object, there is an instance of the ExampleProgram.java class in memory as well.

The System class is never instantiated by the ExampleProgram class because it contains only static variables and methods, and therefore, cannot be instantiated by a program, but it is instantiated behind the scenes by the Java virtual machine

Methods and Functions

Once you have created objects, you want them to be able to do something. This is where methods come in. A method in object-oriented programming is a procedure associated with a class. A method defines the behavior of the objects that are created from the class. Another way to say this is that a method is an action that an object is able to perform. The association between method and class is called binding. Consider the example of an object of the type 'person,' created using the person class. Methods associated with this class could consist of things like walking and driving. Methods are sometimes confused with functions, but they are distinct.

A function is a combination of instructions that are combined to achieve some result. A function typically requires some input (called arguments) and returns some results. For example, consider the example of driving a car. To determine the mileage, you need to perform a calculation using the distance driven and the amount of fuel used. You could write a function to do this calculation. The arguments going into the function would be distance and fuel consumption, and the result would be mileage. Anytime you want to determine the mileage, you simply call the function to perform the calculation.

How does this differ from a method? A function is independent and not associated with a class. You can use this function anywhere in your code, and you don't need to have an object to use it.

Now, what if you were to associate the function with an object of the type 'car?' For example, you want to be able display the mileage of the car on the dashboard. In this case, the mileage calculation has become a method because it is a procedure associated with the car's class. Every time you create a new object of the type 'car' using the car class, this method will be part of the object. The action the car is now able to perform is to calculate mileage. It is the same calculation as performed by the stand-alone function but is now bound to the car.

Well-Defined Boundaries and Cooperation

Class definitions must allow objects to cooperate during execution. In the previous section, you saw how the System, String, and StringBuffer objects cooperated to print a concatenated character string to the command line.

This section changes the example program to display the concatenated character string in a JLabel component in a user interface to further illustrate the concepts of well-defined class boundaries and object cooperation.

The program code to place the text in a label to display it in a user interface uses a number of cooperating classes. Each class has its own function and purpose as summarized below, and where appropriate, the classes are defined to work with objects of another class.

Inheritance

One object-oriented concept that helps objects work together is inheritance. Inheritance defines relationships among classes in an object-oriented language. In the Java programming language, all classes descend from java.lang.Object and implement its methods.

The following diagram shows the class hierarchy as it descends from java.lang.Object for the classes in the user interface example above. The java.lang.Object methods are also shown because they are inherited and implemented by all of its subclasses, which is every class in the Java API libraries. java.lang.Object defines the core set of behaviors that all classes have in common.

As you move down the hierarchy, each class adds its own set of class-specific fields and methods to what it inherits from its superclass or superclasses. The java.awt.swing.JFrame class inherits fields and methods from java.awt.Frame, which inherits fields and methods from java.awt.Container, which inherits fields and methods from java.awt.Component, which finally inherits from java.lang.Object, and each subclass adds its own fields and methods as needed.

Polymorphism

Another way objects work together is to define methods that take other objects as parameters. You get even more cooperation and efficiency when the objects are united by a common superclass. All classes in the Java programming language have an inheritance relationship.

For example

if you define a method that takes a java.lang.Object as a parameter, it can accept any object in the entire Java platform. If you define a method that takes a java.awt.Component as a parameter, it can accept any component object. This form of cooperation is called polymorphism.

Data Access Levels

Another way classes work together is through access level controls. Classes, and their fields and methods have access levels to specify how they can be used by other objects during execution, While cooperation among objects is desirable, there are times when you will want to explicitly control access, and specifying access levels is the way to gain that control. When you do not specify an access level, the default access level is in effect.

Classes

By default, a class can be used only by instances of other classes in the same package. A class can be declared public to make it accessible to all class instances regardless of what package its class is in. You might recall that in Part 1, Part 1, Lesson 3: Building Applets, the applet class had to be declared public so it could be accessed by the appletviewer tool because the appletviewer program is created from classes in another package.

Here is an applet class declared to have a public access level:

publicpackagepackageprotectedprivate

Fields and Methods

Fields and methods can be declared private, protected, public, or package. If no access level is specified, the field or method access level is package by default.

private: A private field or method is accessible only to the class in which it is defined. In Part 1, Lesson 7: Database Access and Permissions the connection, user name, and password for establishing the database access are all private. This is to prevent an outside class from accessing them and jeopardizing the database connection, or compromising the secret user name and password information.

protected: A protected field or method is accessible to the class itself, its subclasses, and classes in the same package.

public: A public field or method is accessible to any class of any parentage in any package. In Part 2, Lesson 6: Internationalization server data accessed by client programs is made public.

package: A package field or method is accessible to other classes in the same package.

Your Own Classes

When you use the Java API library classes, they have already been designed with the above concepts in mind. They all descend from java.lang.Object giving them an inheritance relationship; they have well-defined boundaries; and they are designed to cooperate with each other where appropriate.

But what about when you write your own classes? How can you be sure your classes have well-defined boundaries, cooperate, and make use of inheritance? One way is to look at the functions you need a program to perform and separate them into distinct modules where each functional module is defined by its own class or group of classes.

Well-Defined and Cooperating Classes

Getting data, displaying the data, and resetting the display are closely related and easily form a functional module. But in a larger program with more data processing, the storing and printing of customer IDs could be expanded to store and print a wider range of data. In such a case, it would make sense to have a separate class for storing data, and another class for printing it in various forms.

You could, for example, have a class that defines how to store customer IDs, and tracks the number of apples, peaches, and pears sold during the year. You could also have another class that defines report printing. It could access the stored data to print reports on apples, peaches, and pears sold by the month, per customer, or throughout a given season.

Making application code modular by separating out functional units makes it easier to update and maintain the source code. When you change a class, as long as you did not change any part of its public interface, you only have to recompile that one class.

Inheritance

Deciding what classes your program needs means separating functions into modules, but making your code more efficient and easier to maintain means looking for common functions where you can use inheritance. If you need to write a class that has functionality similar to a class in the Java API libraries, it makes sense to extend that API library class and use its methods rather than write everything from scratch.

Access Levels

You should always keep access levels in mind when you declare classes, fields, and methods. Consider which objects really need access to the data, and use packages and access levels to protect your application data from all other objects executing in the system.

Most object-oriented applications do not allow other objects to access their fields directly by declaring them private. Then they make their methods protected, public, or package as needed and allow other objects to manipulate their private data by calling the methods only. This way, you can update your class by changing a field definition and the corresponding method implementation, but other objects that access that data do not need to be changed because their interface to the data (the method signature) has not changed.

  class ExampleProgram {    public static void main(String[] args){      System.out.println("I'm a simple Program");    }  }