Question

In: Computer Science

Does Java support referential transparency? What about in static contexts like a? What about in object...

Does Java support referential transparency? What about in static contexts like a? What about in object contexts like b? Explain using the following bits of code to explain your answer for each question.

a)

  • static int a,b,c;//declaration
  • //some stuff happens
  • result1 = (fun(a) + b) / (fun(a) – c);
  • temp = fun(a);
  • result2 = (temp + b) / (temp – c);
  • //function signature
  • int static fun(int arg);

b)

  • Employee a = new Employee();
  • Employee b = new Employee();
  • Employee c = new Employee();
  • //some stuff happens
  • result1 = (a.fun() + b) / (a.fun() – c);
  • temp = a.fun();
  • result2 = (temp + b) / (temp – c);
  • //function signature inside Employee:
  • int fun()

Solutions

Expert Solution

Q) Does Java support referential transparency?

A)
Yes, in java functional programming, we make use of PURE functions which will not have any side effect on the program. In referential transparency, some block of code does not affect the program's functioning when it return a different value. Therefore, it is an independent block of code which would not affect the functioning of the program.
Ex-> Utility methods to format amount or date.


Q) What about in static contexts like a?

A)
In case of static context, the variables are bound to the class and not to the object and will share a common property across all the objects of the same class.
Here, we can replace temp = fun(a) with a value that the fun function returns corresponding to value of a. Thus, referential integrity is maintained.
Here, the fun method is a PURE function which returns some value based on the input


Q) What about in object contexts like b?

A)
In case of object context, we never know what the values of a.fun() can be. As it is the method bound to the object, its value cannot be changed with some same integer. Because, a.fun() is not a PURE function. Calling a.fun() everytime will not give us the same result.
Thus, referential integrity is NOT maintained here



- Kindly upvote if this helped


Related Solutions

Which java collection have you used the most? and why? What does static mean in Java...
Which java collection have you used the most? and why? What does static mean in Java and when should you use it? (Both static variable and static method) What does abstract keyword mean in java and when should you use? (Both abstract class and method)
The importance of social support for an infant: what does that look like?
The importance of social support for an infant: what does that look like?
The following Java program is NOT designed using class/object concept. public class demo_Program4_non_OOP_design { public static...
The following Java program is NOT designed using class/object concept. public class demo_Program4_non_OOP_design { public static void main(String[] args) { String bottle1_label="Milk"; float bottle1_volume=250; float bottle1_capacity=500; bottle1_volume=addVolume(bottle1_label, bottle1_volume,bottle1_capacity,200); System.out.println("bottle label: " + bottle1_label + ", volume: " + bottle1_volume + ", capacity: " +bottle1_capacity); String bottle2_label="Water"; float bottle2_volume=100; float bottle2_capacity=250; bottle2_volume=addVolume(bottle2_label, bottle2_volume,bottle2_capacity,500); System.out.println("bottle label: " + bottle2_label + ", volume: " + bottle2_volume + ", capacity: " +bottle2_capacity); } public static float addVolume(String label, float bottleVolume, float capacity, float addVolume)...
Class object in C++ programming language description about lesson static variable example.
Class object in C++ programming language description about lesson static variable example.
What does transparency in reporting mean according to NIH? What is the benefit of using this...
What does transparency in reporting mean according to NIH? What is the benefit of using this principle in biomedical research? How can you apply this to research papers?
What does the static keyword mean?
What does the static keyword mean?
Please explain what static friction is. - If an object slides across a table and stops,...
Please explain what static friction is. - If an object slides across a table and stops, is there static friction still acting on it? Or is there no friction at all? - Please give some examples of static friction and how to identify them in a problem.
DESCRIBE WHAT THE FOLLOWING JAVA CODE DOES: public class Main{ public static void main(String[] args) {...
DESCRIBE WHAT THE FOLLOWING JAVA CODE DOES: public class Main{ public static void main(String[] args) { new MyFrame(); } } import javax.swing.*; public class MyFrame extends JFrame{ MyPanel panel; MyFrame(){ panel = new MyPanel(); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.add(panel); this.pack(); this.setLocationRelativeTo(null); this.setVisible(true); } } import java.awt.*; import javax.swing.*; public class MyPanel extends JPanel{ //Image image; MyPanel(){ //image = new ImageIcon("sky.png").getImage(); this.setPreferredSize(new Dimension(500,500)); } public void paint(Graphics g) { Graphics2D g2D = (Graphics2D) g; //g2D.drawImage(image, 0, 0, null); g2D.setPaint(Color.blue); g2D.setStroke(new BasicStroke(5)); g2D.drawLine(0, 0, 500,...
In Java, explain the concept of a business object. What is the purpose of a business...
In Java, explain the concept of a business object. What is the purpose of a business object?
This question is about the java Object-Oriend program and give the code as follow requirment: You...
This question is about the java Object-Oriend program and give the code as follow requirment: You are a software engineer who has to write the software for a car transmission. The transmission has five gears numbered from 1 to 5 to move forward, one neutral gear position numbered 0 where the car does not move, and one reverse gear numbered -1 to move backward. The transmission has a clutch, and the driver of the car can only change gear if...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT