How to draw the heap for an array in Java?

I have an assignment to draw the heap after an ArrayList and a LinkedList is created.

public static void main(String[] args) {      List list = new ArrayList();      list.add(0);      list.add(1);      list.add(1);      list.add(2); } 

and

public static void main(String[] args) {      List list = new LinkedList();     list.add(0);      list.add(1);      list.add(1);      list.add(2); } 

So far I have a tree set up for [0, 1, 1, 2] so that it just goes down like a tree from 0, 1 and 1, then 2. But I don’t know if that’s correct and if it’d be different for the ArrayList and LinkedList.

How do I approach towards an abstract production rule interpreter with this situation of converting XML to python or java class?

If I am asking in the wrong place, please forgive and direct me to a more suitable one

So I have a XML like this

<range>    unconstrained    <span>       <rttype>          String </range>  <range>    x type    <span>       <rttype>          int       <assert>          $   > 0 </range>  <range>    Simple class reference    <span>       <rttype>          SimpleClass </range>  <range>    Simple class set    <span>       <rttype>          ArrayList<SimpleClass> </range>  <class>   Simple class     <attribute>      x         <range>            x type   </attribute>    <attribute>      state   </attribute>    <action>      initializer      <guarantees>         x has been set to zero      </guarantees>      <pimaction>         .@a x @ = 0      </pimaction>   </action>    <action>      update x      <parameter>         new x         x type      <guarantees>         x has been set to new x      </guarantees>      <pimaction>         .@a x @ = @i new x @      </pimaction>   </action>    <state>      Exists   </state>    <state>      Doesn't exist   </state>    <event>      <<new>>   </event>    <event>      <<destroy>>   </event>    <event>      update   </event>    <transition>      Doesn't exist      <<new>>      Exists      <transitionaction>         initializer   </transition>    <transition>      Exists      <<destroy>>      Doesn't exist   </transition>    <transition>      Exists      update      Exists      <transitionaction>         update x   </transition> 

I have a Java compiler (let’s call this ToJavaCompiler) that will compile this into a Java class

And another Java compiler (let’s call this ToPythonCompiler) that will also compile this into a Python class.

class SimpleClass:      # State Enum Declaration     # see MMClass.ruleStateEnumDeclaration for implementation      SimpleClass_states = Enum("SimpleClass_states", "EXISTS DOESNTEXIST")      # Attribute instance variables     # see MMClass.ruleAttributeInstVarList for implementation      _x: int     _state: SimpleClass_states      # Class level attribute     # All class members accessor      SimpleClassSet: ClassVar[List[SimpleClass]] = []       # Constructor     # See MMClass.ruleConstructorOperation      # See constructEvent.ruleConstructorOperation     def __init__(self):         # requires         #    none         # guarantees         #    --> x has been set to zero and state == Exists         self._initializer()         self._state = SimpleClass.SimpleClass_states.EXISTS         SimpleClass.SimpleClassSet.append(self)      # Attribute getters      @property     def x(self) -> int:         # requires         #   none         # guarantees         #   returns the x         return self._x      @property     def state(self) -> SimpleClass_states:         # requires         #   none         # guarantees         #   returns the state         return self._state       # Pushed events      def destroy(self) -> None:         # requires         #    none         # guarantees         #   state was Exists --> state == Doesn't exist         if self._state == SimpleClass.SimpleClass_states.EXISTS:             self._state = SimpleClass.SimpleClass_states.DOESNTEXIST             SimpleClass.SimpleClassSet.remove(self)      def update(self, new_x: int) -> None:         # requires         #    none         # guarantees         #   state was Exists --> x has been set to new x         if self._state == SimpleClass.SimpleClass_states.EXISTS:             self._update_x(new_x)      # Private transition actions      def _initializer(self):         # requires         #   none         # guarantees         #   x has been set to zero         self._x = 0      def _update_x(self, new_x: int):         # requires         #   none         # guarantees         #   x has been set to new x         self._x = new_x 

THe thing is my production rule need access to instance variable data from the model object they are compiling.

For example to generate the instance variables declarations i need a production rule that’s written in Java code like this which require access to the underlying model itself at Context.model()

public void ruleAttributeInstVarList() {     // description     // this rule emits the set of (private) instance variable declarations, if any     //     // Class.#ATTRIBUTE_INST_VAR_LIST -->     // foreach anAttribute in class     // anAttribute.#DEFINE_INST_VAR     //     // requires     // none     // guarantees     // all attributes of this class have been declared as instance variable of the     // PIM Overlay run-time type     if (Context.model().isVerbose()) {         Context.codeOutput().indent();         Context.codeOutput().println("# Attribute instance variables");         Context.codeOutput().println("# see MMClass.ruleAttributeInstVarList for implementation");                  Context.codeOutput().println("");         if (!attributeSet.isEmpty()) {             for (Attribute anAttribute : attributeSet) {                 anAttribute.ruleDefineInstVarAsPrivate();             }         } else {             if (Context.model().isVerbose()) {                 Context.codeOutput().indent();                 Context.codeOutput().println("# none");             }         }         Context.codeOutput().indentLess();     } else {         for (Attribute anAttribute : attributeSet) {             anAttribute.ruleDefineInstVarAsPrivate();         }     }     Context.codeOutput().println("");     Context.codeOutput().println(""); } 

I wonder if there’s an easier way to add target languages or frameworks without creating separate codebases per target language.

For e.g. I now have a ToJavaCompiler and a ToPythonCompiler two separate codebases

So I am here asking if there’s a way I can create an abstract production rule interpreter that suits my needs. My aim is to ultimately produce model classes in the target language or framework (such as Django or Rails) with a single codebase that allows extensions for different target languages/frameworks

I am okay to move away from Java if there’s a better language that suits what I am trying to do.

Canvas VS JPanel (Java)

Everyone says all the time – "Be careful to combine Canvas with Swing components, because it is a heavyweight component and Swing is lightweight". How exactly will this harm me? Maybe in very specific cases that do not even come to my mind, but currently Canvas is the best way to develop games in Java if you use Swing in my opinion. And the fact is, Minecraft is also painted on AWT’s Canvas. Correct me if I’m wrong. But right now what gives me more control is Canvas and not JPanel.

Java Programming

Assume that you have been assigned a task to develop an application which will be handling teaching activities as the University. The application has four main entities namely administrator, lecturer, student and course. The application should be able to allow: a) Registration of classes b) Registration of modules c) Registration of lecturers d) Registration of students e) Assignment of modules into class f) Assignment of module to lecturer g) Assignment of student into class h) View all registered students, modules, classes i) View all students and their modules j) View lecturers and their modules The application should be developed by using Object Oriented programming in which encapsulation, inheritance, abstraction and polymorphism concepts MUST be observed and implemented by using Java

Establishing safe connection in Java

In my Java project I’m trying to create a 100% secure method of communication between the method and the client. I used to use this process:

Client: generates 4096-bit RSA keypair Client: sends public to server Client: generates 256-bit AES key Client: encrypts AES key using RSA and send Server: decrypts AES key from RSA Server & Client now communicate using AES only 

But I found out this isn’t safe as it can easily be ruined by a man-in-the-middle attack. I began researching TLS and found out about security certificates. My question is this: if the client generates the RSA keypair, signs it using the certificate, and sends it to the server, what stops a MITM from doing the same thing (assuming the certificate is publicly available, which I assume it would be because the server and the client would both need it).

When I use OpenSSL to generate a certificate it always provides an RSA key alongside it. Isn’t it safer to generate a new keypair for each connection, or do I actually use this particular key? What am I missing about the standard pattern for TLS?

From Python to Java or C++? [closed]

I was learning Python 3 since the Last 2 years and I have covered many topics in python. And Python is the 1st ever language I have learnt ( i really had tough times to learn python)

Now, I want to participate in a coding contest like USACO, there python is not allowed so I want to learn Java/C++

But I am Confused about Which is the best way to learn and from where to learn? How long will it take to learn?should I learn Java Or C++ which one is better for future demand?

And How hard is it to transfer my python knowledge to Learn Java/C++?

About Me In Future, I would like to work with the Theoretical Computer Science and Solve Real World Problems and Develop New Technologies .

(I do not want to be Software Engineer )

How do I fix ‘Could not determine java version from ‘14.0.2’.’ when trying to make Minecraft mods?

I try to build my mod like this.

I get the error:

FAILURE: Build failed with an exception.

  • What went wrong: Could not determine java version from ‘14.0.2’.

What causes this? How do I fix this? I never used Gradle or Java before so please be specific. Is this caused because Gradle doesn’t support Java 14.0.2? If so, how do I tell Gradle to use a Java version it does support? I’m a noob. I have no experience with Gradle at all, I don’t even know what it is or what it’s for, all I know is I’m trying to make a basic Minecraft mod. However I do know Java.

what is wrong in following code in Java (Switching cases)?

package ifelse; import java.util.Scanner; import java.util.*;  public class SwitchCasese {     public static void main(String[] args) {         Scanner sc = new Scanner (System.in);         System.out.println("enter your day ");         String day = sc.nextLine();         String result;                  switch (day){         case "monday ":             result = "working";             break;         case "Tuesday":             result = "Studying";             break;         case "wednesday":             result = "riding";             break;                                   }            System.out.println(result );                            }  } ``` 

Deterministic Finite Automata vs Java

You need to select a device controller. You have two options: Option 1: Implement with a DFA Option 2: Implement using Java The primary advantage of a DFA over a program written in Java is as follows:

Answer choices:

  • A DFA requires fewer computational resources
  • A DFA is faster than a program in Java
  • Running a DFA costs less than running a program written in Java
  • It doesn’t matter if we use a DFA or a program written in Java, as long as it gets the job done