CS 330 Lecture 11 – Hi, C
Agenda quiz review Call subclass in Flop the birth of C compiling printf sizeof functions and forward declarations scanf getting addresses with & pointers Code first.c #include <stdio.h> // —————————————————————————- // Function declarations // —————————————————————————- void clamp(int *number); void clampByValue(int number); // —————————————————————————- // Function implementations // —————————————————————————- int main(int argc, char **argv) { double […]
CS 330 Lecture 10 – Translating to an AST
Agenda adding functions to Flop storing parse results in an AST quiz Code Flop.g grammar Flop; @header { import java.util.HashMap; } @members { HashMap<String, Integer> memory; HashMap<String, Expression> functions; public abstract class Expression { public abstract int evaluate(); } public class Literal extends Expression { private int value; public Literal(int value) { this.value = value; […]
CS 145 Lab 4 – Writing methods
Prelab Complete Self-check 3.13 on the Practice-It website before 8 AM on February 6. Introduction Last time we looked at calling methods that were available in the String, Scanner, and Random classes. This week, we write our own methods. Reminder: Be sure to get your checkpoints from lab 2 checked off in the first 20 […]
CS 145 Lecture 6 – Methods 2
Agenda what does this do? the separation between caller and callee building a snowman method chaining What does this do? Code . <h4>Area.java</h4> <pre class=”code”> package preexam1; public class Area { public static void main(String[] args) { double r1 = 5; // java.awt.Toolkit.getDefaultToolkit().beep(); double area = getArea(8); System.out.println(r1); } public static double getArea(double radius) { […]
CS 330 Lecture 9 – Calculator + functions
Agenda evaluating expressions persistence of memory adding in functions avoiding reparsing with abstract syntax trees (ASTs) Code Flop.g grammar Flop; @header { import java.util.HashMap; } @members { HashMap<String, Integer> memory; public void setMemory(HashMap<String, Integer> memory) { this.memory = memory; } } stat : ID EQUALS expr {memory.put($ID.text, $expr.value);} | expr {System.out.println($expr.value);} ; expr returns [int […]
CS 145 Lecture 5 – Methods
Agenda print vs. println Scanner oddities don’t repeat yourself methods: hide details encapsulate repeatable processes parameters and return types Code CharacterArithmetic.java package preexam1; public class CharacterArithmetic { public static void main(String[] args) { char c = ‘m’; System.out.print(c + 5); System.out.println(” <- the sum”); c = (char) (c + 5); System.out.println(c); char avgLetter = (‘a’ […]
CS 330 Lecture 8 – A calculator language
Agenda writing a calculator language — with variables! forcing precedence the hazard of recursive descent generalizing rules in make Code … Haiku
CS 330 Lecture 7 – Finishing SLOWGO, a first Makefile, and a calculator
Agenda smart compilation with Make add drawing in to SLOWGO Code makefile ANTLRWORKS = $(HOME)/bin/antlrworks-1.4.3.jar SLOWGOInterpreter.class: SLOWGOInterpreter.java SLOWGOLexer.class SLOWGOParser.class javac -cp $(ANTLRWORKS):. SLOWGOInterpreter.java SLOWGOLexer.class: SLOWGOLexer.java javac -cp $(ANTLRWORKS) SLOWGOLexer.java SLOWGOParser.class: SLOWGOParser.java javac -cp $(ANTLRWORKS) SLOWGOParser.java SLOWGOLexer.java SLOWGOParser.java: SLOWGO.g java -cp $(ANTLRWORKS) org.antlr.Tool SLOWGO.g clean: rm -f *.class SLOWGO.g grammar SLOWGO; @members { private int x […]
CS 145 Lecture 4 – String problems and our first method
Agenda extracting names: “LASTNAME, FIRSTNAME MIDDLENAME …” lock the web: does a URL end in .edu? separate a comma-separated list turn “1,234,567” into 1234567 abstraction: hiding details abstracting SVG Code NameFixerUpper.java package preexam1; public class NameFixerUpper { public static void main(String[] args) { String name = “STEINMEYER, JOEL EDWARD”; // names in bad order! // […]