SlideShare une entreprise Scribd logo
1  sur  3
Télécharger pour lire hors ligne
The separation of data from its view is referred to as the Model-View pattern (Often, a third
element is
added to create the Model-View-Controller pattern).
In the code segment below, the class Scores is the model where the addScore() method changes
the
scoreList[] by adding a score that is passed in as a parameter. When the model changes it
notifies its
observers. It is required that the class ConsoleShow should be an observer on the changes in
Scores;
when the observer is notified of the changes, the method, procScores() should be executed.
a) Make the required changes to ConsoleShow to make it an Observer. You cannot add, remove
or
change the methods defined in these classes. Therefore you can only make changes to the first
line in the class definitions (class ConsoleShow ....).
b) You can make changes to class ProcessScores.
c) You can make changes to only the first line of class Scores. You cannot make any other
changes
to the rest of the class definition.
d) In main(), a Scores object (model), scoreOb and one observer object, one are created.
Complete
the code segment (add lines) such that one becomes an observer on the model scoreOb. You
cannot modify or delete lines 1, 2 or 3 (line numbers in comments) in this code segment.
e) When line 3 (line number in comments) is executed, show what is displayed on the console?
abstract class ProcessScores implements Observer{
public void update(Observable o, Object arg) {
if (o == null) System.out.println("o null");
procScores(o);
}
}
class ConsoleShow {
public void procScores(Object arg){
/* Display a pattern based on the sum in Scores object */
if (arg == null) System.out.println("1: Exiting");
else{
for (int i = 0; i < ((Scores)arg).getSum(); ++i){
for (int j = 0; j <= i; ++j)
System.out.print("*");
System.out.println();
}
}
}
}
class Scores {
final int max = 10;
private int scoreList[] = new int[max];
private int sum = 0;
public void addScore(int index, int score) {
if (index < max){
scoreList[index] = score;
sum = 0;
for (int i = 0; i < max; ++i)
sum += scoreList[i];
// Notify the registered observers
setChanged();
notifyObservers();
}
}
public String toString(){return new Integer(sum).toString();}
public int getSum(){ return sum;}
}
public class Question5 {
public static void main(String [] args) {
// Create an instance of the model
/* 1 */ Scores scoreOb = new Scores();
// Create an instance of ConsoleShow observer
/* 2 */ ProcessScores one = new ConsoleShow();
// Now add a score to scoreOb. This should set off notifications
// to the observers.
/* 3 */ scoreOb.addScore(5,6);
}
}

Contenu connexe

Similaire à The separation of data from its view is referred to as the Model-Vie.pdf

Dotnet unit 4
Dotnet unit 4Dotnet unit 4
Dotnet unit 4
007laksh
 

Similaire à The separation of data from its view is referred to as the Model-Vie.pdf (20)

39927902 c-labmanual
39927902 c-labmanual39927902 c-labmanual
39927902 c-labmanual
 
Vb net1
Vb net1Vb net1
Vb net1
 
Spring aop
Spring aopSpring aop
Spring aop
 
20.1 Java working with abstraction
20.1 Java working with abstraction20.1 Java working with abstraction
20.1 Java working with abstraction
 
02.adt
02.adt02.adt
02.adt
 
Java file
Java fileJava file
Java file
 
Java file
Java fileJava file
Java file
 
The Ring programming language version 1.3 book - Part 83 of 88
The Ring programming language version 1.3 book - Part 83 of 88The Ring programming language version 1.3 book - Part 83 of 88
The Ring programming language version 1.3 book - Part 83 of 88
 
Introduction to python programming
Introduction to python programmingIntroduction to python programming
Introduction to python programming
 
Csphtp1 03
Csphtp1 03Csphtp1 03
Csphtp1 03
 
Dotnet unit 4
Dotnet unit 4Dotnet unit 4
Dotnet unit 4
 
Python for Beginners(v2)
Python for Beginners(v2)Python for Beginners(v2)
Python for Beginners(v2)
 
Introduction to the Stat-JR software package
Introduction to the Stat-JR software packageIntroduction to the Stat-JR software package
Introduction to the Stat-JR software package
 
Java programs
Java programsJava programs
Java programs
 
Templates exception handling
Templates exception handlingTemplates exception handling
Templates exception handling
 
Java Unit 1 Project
Java Unit 1 ProjectJava Unit 1 Project
Java Unit 1 Project
 
Java practical
Java practicalJava practical
Java practical
 
14 operator overloading
14 operator overloading14 operator overloading
14 operator overloading
 
Bis 311 final examination answers
Bis 311 final examination answersBis 311 final examination answers
Bis 311 final examination answers
 
Major Java 8 features
Major Java 8 featuresMajor Java 8 features
Major Java 8 features
 

Plus de ambikagarmentsjdp

The role of government in promoting business ethicsActivity 2R.pdf
The role of government in promoting business ethicsActivity 2R.pdfThe role of government in promoting business ethicsActivity 2R.pdf
The role of government in promoting business ethicsActivity 2R.pdf
ambikagarmentsjdp
 
Then we need the rules that will allow for the queries that will ans.pdf
Then we need the rules that will allow for the queries that will ans.pdfThen we need the rules that will allow for the queries that will ans.pdf
Then we need the rules that will allow for the queries that will ans.pdf
ambikagarmentsjdp
 
The X axis shows how far away from the equator the population lives..pdf
The X axis shows how far away from the equator the population lives..pdfThe X axis shows how far away from the equator the population lives..pdf
The X axis shows how far away from the equator the population lives..pdf
ambikagarmentsjdp
 

Plus de ambikagarmentsjdp (20)

The speed control of an induction motor has input signals defined as.pdf
The speed control of an induction motor has input signals defined as.pdfThe speed control of an induction motor has input signals defined as.pdf
The speed control of an induction motor has input signals defined as.pdf
 
The Smith manufacturing company has estimated the following componen.pdf
The Smith manufacturing company has estimated the following componen.pdfThe Smith manufacturing company has estimated the following componen.pdf
The Smith manufacturing company has estimated the following componen.pdf
 
The Scientific Revolution led directly to the cultural period that f.pdf
The Scientific Revolution led directly to the cultural period that f.pdfThe Scientific Revolution led directly to the cultural period that f.pdf
The Scientific Revolution led directly to the cultural period that f.pdf
 
The role of government in promoting business ethicsActivity 2R.pdf
The role of government in promoting business ethicsActivity 2R.pdfThe role of government in promoting business ethicsActivity 2R.pdf
The role of government in promoting business ethicsActivity 2R.pdf
 
The Rectangle class headerAnd base class Shape headerThe code co.pdf
The Rectangle class headerAnd base class Shape headerThe code co.pdfThe Rectangle class headerAnd base class Shape headerThe code co.pdf
The Rectangle class headerAnd base class Shape headerThe code co.pdf
 
Thermometer measurements have tracked Earth�s surface temperature. W.pdf
Thermometer measurements have tracked Earth�s surface temperature. W.pdfThermometer measurements have tracked Earth�s surface temperature. W.pdf
Thermometer measurements have tracked Earth�s surface temperature. W.pdf
 
These are some of the contents in passwd2 fileIt has 1851 lines .pdf
These are some of the contents in passwd2 fileIt has 1851 lines .pdfThese are some of the contents in passwd2 fileIt has 1851 lines .pdf
These are some of the contents in passwd2 fileIt has 1851 lines .pdf
 
These are some of the contents in passwd2 file It has 1851 lines.pdf
These are some of the contents in passwd2 file It has 1851 lines.pdfThese are some of the contents in passwd2 file It has 1851 lines.pdf
These are some of the contents in passwd2 file It has 1851 lines.pdf
 
There is no more information provided to me other then what i alread.pdf
There is no more information provided to me other then what i alread.pdfThere is no more information provided to me other then what i alread.pdf
There is no more information provided to me other then what i alread.pdf
 
There is no more information provided to me. Determine the value of .pdf
There is no more information provided to me. Determine the value of .pdfThere is no more information provided to me. Determine the value of .pdf
There is no more information provided to me. Determine the value of .pdf
 
There is a period between when animals become infected and when they.pdf
There is a period between when animals become infected and when they.pdfThere is a period between when animals become infected and when they.pdf
There is a period between when animals become infected and when they.pdf
 
There is a link between security and quality.1. Examine the relati.pdf
There is a link between security and quality.1. Examine the relati.pdfThere is a link between security and quality.1. Examine the relati.pdf
There is a link between security and quality.1. Examine the relati.pdf
 
There are advantages to buying an existing business. Which of the fo.pdf
There are advantages to buying an existing business. Which of the fo.pdfThere are advantages to buying an existing business. Which of the fo.pdf
There are advantages to buying an existing business. Which of the fo.pdf
 
There are different types of Intrusion DetectionPrevention Systems..pdf
There are different types of Intrusion DetectionPrevention Systems..pdfThere are different types of Intrusion DetectionPrevention Systems..pdf
There are different types of Intrusion DetectionPrevention Systems..pdf
 
There are 4200 college instructors in Canada that teach at local com.pdf
There are 4200 college instructors in Canada that teach at local com.pdfThere are 4200 college instructors in Canada that teach at local com.pdf
There are 4200 college instructors in Canada that teach at local com.pdf
 
Then we need the rules that will allow for the queries that will ans.pdf
Then we need the rules that will allow for the queries that will ans.pdfThen we need the rules that will allow for the queries that will ans.pdf
Then we need the rules that will allow for the queries that will ans.pdf
 
The X axis shows how far away from the equator the population lives..pdf
The X axis shows how far away from the equator the population lives..pdfThe X axis shows how far away from the equator the population lives..pdf
The X axis shows how far away from the equator the population lives..pdf
 
The World Values Survey interviewed a random sample of people from n.pdf
The World Values Survey interviewed a random sample of people from n.pdfThe World Values Survey interviewed a random sample of people from n.pdf
The World Values Survey interviewed a random sample of people from n.pdf
 
The Yellow Company made year-end adjusting entries affecting each of.pdf
The Yellow Company made year-end adjusting entries affecting each of.pdfThe Yellow Company made year-end adjusting entries affecting each of.pdf
The Yellow Company made year-end adjusting entries affecting each of.pdf
 
The Worst Part Was That She Often Was Right. The Winston County Bank.pdf
The Worst Part Was That She Often Was Right. The Winston County Bank.pdfThe Worst Part Was That She Often Was Right. The Winston County Bank.pdf
The Worst Part Was That She Often Was Right. The Winston County Bank.pdf
 

Dernier

1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
heathfieldcps1
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
Chris Hunter
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 

Dernier (20)

Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIFood Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 

The separation of data from its view is referred to as the Model-Vie.pdf

  • 1. The separation of data from its view is referred to as the Model-View pattern (Often, a third element is added to create the Model-View-Controller pattern). In the code segment below, the class Scores is the model where the addScore() method changes the scoreList[] by adding a score that is passed in as a parameter. When the model changes it notifies its observers. It is required that the class ConsoleShow should be an observer on the changes in Scores; when the observer is notified of the changes, the method, procScores() should be executed. a) Make the required changes to ConsoleShow to make it an Observer. You cannot add, remove or change the methods defined in these classes. Therefore you can only make changes to the first line in the class definitions (class ConsoleShow ....). b) You can make changes to class ProcessScores. c) You can make changes to only the first line of class Scores. You cannot make any other changes to the rest of the class definition. d) In main(), a Scores object (model), scoreOb and one observer object, one are created. Complete the code segment (add lines) such that one becomes an observer on the model scoreOb. You cannot modify or delete lines 1, 2 or 3 (line numbers in comments) in this code segment. e) When line 3 (line number in comments) is executed, show what is displayed on the console? abstract class ProcessScores implements Observer{ public void update(Observable o, Object arg) { if (o == null) System.out.println("o null"); procScores(o); } } class ConsoleShow { public void procScores(Object arg){ /* Display a pattern based on the sum in Scores object */ if (arg == null) System.out.println("1: Exiting"); else{
  • 2. for (int i = 0; i < ((Scores)arg).getSum(); ++i){ for (int j = 0; j <= i; ++j) System.out.print("*"); System.out.println(); } } } } class Scores { final int max = 10; private int scoreList[] = new int[max]; private int sum = 0; public void addScore(int index, int score) { if (index < max){ scoreList[index] = score; sum = 0; for (int i = 0; i < max; ++i) sum += scoreList[i]; // Notify the registered observers setChanged(); notifyObservers(); } } public String toString(){return new Integer(sum).toString();} public int getSum(){ return sum;} } public class Question5 { public static void main(String [] args) { // Create an instance of the model /* 1 */ Scores scoreOb = new Scores(); // Create an instance of ConsoleShow observer /* 2 */ ProcessScores one = new ConsoleShow(); // Now add a score to scoreOb. This should set off notifications // to the observers. /* 3 */ scoreOb.addScore(5,6); }
  • 3. }