COMP 274 Week 3 labs: 25 points - Programming Exercises 8.11 25 points - Program
ID: 3537014 • Letter: C
Question
COMP 274 Week 3 labs:
25 points - Programming Exercises 8.11
25 points - Programming Exercises 10.3
Points distribution:
Lab Report Questions 20% (Template is in Doc Sharing)
Comments 20%
No Syntax (Complier) error 20%
No Runtime (system) error 20%
No Logical Error (correct results) 20%
8.11 Array of Objects
In Chapter 6, %u201CSingle-Dimensional Arrays,%u201D arrays of primitive type elements were created. You can also create arrays of objects. For example, the following statement declares and creates an array of ten Circle objects:
Circle[] circleArray = new Circle[10];
To initialize the circleArray, you can use a for loop like this one:
for (int i = 0; i < circleArray.length; i++) {
circleArray[i] = new Circle();
}
An array of objects is actually an array of reference variables. So, invoking circleArray[1].getArea() involves two levels of referencing, as shown in Figure 8.18. circleArray references the entire array. circleArray[1] references a Circle object.
-------------------------------------------------------------------------------------------------------------------------------
10.3 The Scope of Variables
Chapter 5, %u201CMethods,%u201D discussed local variables and their scope rules. Local variables are declared and used inside a method locally. This section discusses the scope rules of all the variables in the context of a class.
Instance and static variables in a class are referred to as the class%u2019s variables or data fields. A variable defined inside a method is referred to as a local variable. The scope of a class%u2019s variables is the entire class, regardless of where the variables are declared. A class%u2019s variables and methods can appear in any order in the class, as shown in Figure 10.1(a). The exception is when a data field is initialized based on a reference to another data field. In such cases, the other data field must be declared first, as shown in Figure 10.1(b). For consistency, this book declares data fields at the beginning of the class.
Explanation / Answer
8.11)
Anexample of array of objects for employee class
class Employee
{
protected String name;
protected String jobTitle;
Employee(String n, String title)
{
name = n;
jobTitle = title;
}
}
public class Company
{
private String CompanyName;
private String CompanyAdress;
private Employee employeesList[] = new Employee[3];
public Company (String CompanyName , String CompanyAdress, String PersonOne, String personTwo ,String personThree ,String personFour ,String employeesTitles[])
{
this.CompanyName = CompanyName;
this.CompanyAdress = CompanyAdress;
this.employeesList[0].name = PersonOne;
this.employeesList[1].name = personTwo;
this.employeesList[2].name = personThree;
this.employeesList[3].name = personFour;
}
public void ShowDetails()
{
System.out.println("CompanyName : " + CompanyName + " CompanyAdress : "+CompanyAdress+ "Employee Names "+ employeesList[0].name+employeesList[1].name+employeesList[2].name+employeesList[3].name);
}
}
10.3 )
Scope of variable:
Once deployed the scope of class ThirdClass seems to be application deployment level. And the static value of var was retained while multiple method calls.
public class ThirdClass
{
public static var = "Java";
}
public class Second class
{
public void testA
{
ThirdClass ob1 = new ThirdClass(); // not needed , just kept for clarity.
System.out.println(ThirdClass.var);
ThirdClass.var="Ruby";
ThirdClass ob2 = new ThirdClass();
System.out.println(ThirdClass.var);
}
public class First
{
public void testB
{
ThirdClass ob3 = new ThirdClass();
System.out.println(ThirdClass.var);
ThirdClass.var="CSHARP";
}
public void testC
{
ThirdClass ob4 = new ThirdClass();
System.out.println(ThirdClass.var);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.