Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/391.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JAVA:子类、自学考试、课程作业、家庭作业_Java_Subclass - Fatal编程技术网

JAVA:子类、自学考试、课程作业、家庭作业

JAVA:子类、自学考试、课程作业、家庭作业,java,subclass,Java,Subclass,大家好 作为我自学Java的一部分,我正在尝试完成一个Java begginer作业(非常旧的东西-2001) 问题是,我不知道如何应对这一挑战:(我将感谢任何建议,因为解决方案不再可用,只有压缩档案的链接可以正常工作 问候,, 玛丽 附:任务: **“**作业3:专题3子类的作业 (这是由创建的项目的修订版。) Cindy Norris教授(阿巴拉契亚州立大学CS) 这项作业的目的是让你在子类特别有用的环境中练习它们。你将为最小机器语言MML编写一个解释器。机器语言指令的一般形式是 标签指令寄

大家好

作为我自学Java的一部分,我正在尝试完成一个Java begginer作业(非常旧的东西-2001)

问题是,我不知道如何应对这一挑战:(我将感谢任何建议,因为解决方案不再可用,只有压缩档案的链接可以正常工作

问候,, 玛丽

附:任务:

**“**作业3:专题3子类的作业

(这是由创建的项目的修订版。) Cindy Norris教授(阿巴拉契亚州立大学CS)

这项作业的目的是让你在子类特别有用的环境中练习它们。你将为最小机器语言MML编写一个解释器。机器语言指令的一般形式是

标签指令寄存器列表

label是该行的标签。其他说明可能会“跳转”到该标签

指令是实际的指令。在MML中,有用于加法、乘法等、存储和检索整数以及有条件地分支到其他标签(如if语句)的指令

寄存器列表是指令操作的寄存器列表。寄存器是计算机内存中的简单整数存储区域,与变量非常相似。在MML中,有32个寄存器,编号为0、1、…、31

MML有以下说明:

L1添加r s1 s2-添加寄存器s1和s2的内容,并将结果存储在寄存器r中

L1 sub r s1 s2-从s1的内容中减去寄存器s2的内容,并将结果存储在寄存器r中

L1 mul r s1 s2-将寄存器s1和s2的内容相乘,并将结果存储在寄存器r中

L1 div r s1 s2将寄存器s1的内容除以寄存器s2的内容(Java整数除法),并将结果存储在寄存器r中

L1输出s1-在Java控制台上打印寄存器s1的内容(使用println)

L1 lin r x-将整数x存储在寄存器r中

L1 bnz s1 L2如果寄存器s1的内容不为零,则使标记为L2的语句成为下一个要执行的语句

我们将不同指令的数量保持在较小的范围内,这样您就可以少做一些工作。例如,可能有其他分支指令、否定指令、输入指令等等。但一旦您实现了这一小语言,您就可以轻松地添加更多指令

L1是任何标识符——实际上是任何非空白字符序列。程序的每个语句都必须使用不同的标识符进行标记。s1、s2和r中的每一个都是范围为0..31的整数,并引用机器中执行语言MML的32个寄存器中的一个。下面是一个MML程序计算阶乘6的示例。请注意,指令的相邻字段(标签、操作码和操作数)由空格分隔

f0  lin 20 6
f1  lin 21 1
f2  lin 22 1
f3  mul 21 21 20
f4  sub 20 20 22
f5  bnz 20 f3
f6  out 21
程序的指令按顺序执行(从第一条开始),除非执行bnz指令改变了顺序。执行在最后一条指令执行后终止(且不改变执行顺序)

你的翻译会

  • 从用户处获取包含程序的文件的名称
  • 从文件中读取程序并将其转换为内部形式
  • 打印程序
  • 执行程序,并
  • 打印寄存器的最终值。
  • Machine.java

    import java.util.*;
    
    // The machine language interpreter
    public class Machine {
        // The labels in the MML program, in the order in which
        // they appear (are defined) in the program
        private Labels labels= new Labels(); 
    
        // The MML program, consisting of prog.size() instructions, each
        // of class Instruction (or one of its subclasses)
        private Vector prog= new Vector();   
    
        // The registers of the MML machine
        private Registers registers;
    
        // The program counter; it contains the index (in prog) of
        // the next instruction to be executed.
        private int PC= 0;
    
        public static void main (String[] pars) {
    
            Machine m= new Machine();
            Translator.readAndTranslate(m.labels, m.prog);
    
            System.out.println("Here is the program; it has " +
          m.prog.size() + " instructions.");
            m.print();
            System.out.println();
    
            System.out.println("Beginning program execution.");
            m.execute();
     System.out.println("Ending program execution.");
    
            System.out.println("Values of registers at program termination:");
            System.out.println(m.registers + ".");
     System.exit(0);
        }
    
        // Print the program
        public void print() {
            for (int i= 0; i != prog.size(); i++) {
                System.out.println((Instruction) prog.elementAt(i));
            }
        }
    
        // Execute the program in prog, beginning at instruction 0.
        // Precondition: the program and its labels have been store properly.
        public void execute() {
     PC= 0;
     registers= new Registers();
     while (PC < prog.size()) {
         Instruction ins= (Instruction)prog.elementAt(PC);
         PC= PC+1;
         ins.execute(this);
     }
        }
    
        // = the registers of this machine
        public Registers getRegisters() {
     return registers;
        }
    
        // = the labels of this machine
        public Labels getLabels() {
     return labels;
        }
    
        // Set the program counter to pc
        public void setPC(int pc) {
     PC= pc;
        }
    }
    
    Registers.java


    从作业的角度看,似乎您应该将
    指令
    -外汇分类为子类:

    public class AddInstruction implements Instruction{
    
        public AddInstruction(String l, int r, int s1, int s2) {
            // Store the stuff passed in
        }
    
        public void execute(Machine m) {
            Registers reg = m.getRegisters();
            reg.setRegister(r, reg.getRegister(s1) + reg.getRegister(s2));
        }
    }
    

    第一种方法是阅读说明;)(我刚刚注意到这甚至是一个有用的评论)不太可能有人想坐下来阅读所有这些。一种更有效的方法是发布您遇到的特定问题,并且只发布与此相关的信息。
    import java.util.*;
    
    // An instance contains a list of Strings, called "labels",
    // in the order in which they were added to the list. 
    public class Labels {
        private Vector labels= new Vector();
    
        // Constructor: an empty list of labels
        public Labels() {
        }
    
        // Add label lab to this list and return its number in the list
        // (the first one added is number 0)
        // Precondition: the list has at most 49 entries
        public int addLabel(String lab) {
            labels.addElement(lab);
            return labels.size()-1;
        }
    
        // = the number of label lab in the list
        //   (= -1 if lab is not in the list)
        public int indexOf(String lab) {
    
            // invariant: lab is not in labels[0..i-1]
            for (int i= 0; i != labels.size(); i++) {
                if (lab.equals((String)(labels.elementAt(i)))) {
                    return i;
                }
            }   
            return -1;
        }
    
        // representation of this instance, "(label 0, label 1, ..., label (n-1))"
        public String toString() {
            String r= "(";
            // invariant: r contains the representation for labels[0..i-1]
            // (with the opening "(" but no closing ")")
            for (int i= 0; i != labels.size(); i++) {
                if (i == 0) {
                    r= r + (String)(labels.elementAt(i));
                } else {
                    r= r + ", " + (String)(labels.elementAt(i));
                }
            }
            r= r + ")";
            return r;
        }
    
        // Set the number of elements in the list to 0
        public void reset() {
            labels.removeAllElements();
        }
    }
    
    // An instance contains 31 registers and methods to access
    // and change them
    public class Registers {
        private int registers[]= new int[31];
    
        // Constructor: an instance whose registers are set to 0
        public Registers() {
            for (int i= 0; i != registers.length; i++) {
                registers[i]= 0;
            }
        }
    
        // = the value in register i.
        // Precondition: 0 <= i < 32
        public int getRegister(int i) {
            return registers[i];
        }
    
        // Set register i to v.
        // Precondition: 0 <= i < 32
        public void setRegister(int i, int v) {
            registers[i]= v;
        }
    
        // =  a representation of the registers,
        //    "(reg 0, reg 1, ..., reg 31)"
        public String toString() {
            String r= "(" + registers[0];
            // invariant: r contains the representation for registers[0..i-1]
            // (with the opening "(" but no closing ")")
            for (int i= 1; i != registers.length; i++) {
                r= r + ", " + registers[i];
            }
            r= r + ")";
            return r;
        }
    }
    
    // This class is the superclass of the classes for machine instructions
    public abstract class Instruction {
    
        // Constructor: an instruction with label l and opcode op
        // (op must be an operation of the language)
        public Instruction(String l, String op) {
        }
    
        // = the representation "label: opcode" of this Instruction
        public String toString() {
            return "";
        }
    
        // Execute this instruction on machine m. 
        public abstract void execute(Machine m);
    }
    
    public class AddInstruction implements Instruction{
    
        public AddInstruction(String l, int r, int s1, int s2) {
            // Store the stuff passed in
        }
    
        public void execute(Machine m) {
            Registers reg = m.getRegisters();
            reg.setRegister(r, reg.getRegister(s1) + reg.getRegister(s2));
        }
    }