Hi,
I have just started to learn Java and programming in general from different youtube videos. I am currently working on the assignments from here stanford.edu/class/cs106a/handouts/13-assignment-2.pdf and I would be happy if I could get some feedback on the code I have written.
Assignment 3
import acm.program.*;
public class FindRange extends ConsoleProgram {
public void run() {
println("This program finds the largets and smallest numbers that the user put in, enter 0 to stop the program");
double inmatat = 1;
double large = 0;
double small = 1;
int counter = 0;
while (inmatat != 0){
if (inmatat > large){
large = inmatat;
}
if (counter == 1 ){
small = inmatat;
}
if (inmatat < small){
small = inmatat;
}
counter++;
inmatat = readInt("Enter n1; ");
}
if ( counter !=1){
println("The largets number is " + large);
println("The smallest numbers is " + small);
}
else {
println("The program was stoped without any numbers input from user");
}
}
}
Assignment 4
import acm.graphics.*;
import acm.program.*;
import java.awt.*;
public class Target extends GraphicsProgram {
public void run() {
GOval circle1 = new GOval(100, 20, 144, 144);
circle1.setFilled(true);
circle1.setFillColor(Color.RED);
circle1.setColor(Color.RED);
add (circle1);
GOval circle2 = new GOval(125.2, 45.2, 93.6, 93.6);
circle2.setFilled(true);
circle2.setFillColor(Color.WHITE);
circle2.setColor(Color.WHITE);
add (circle2);
GOval circle3 = new GOval(150.4, 70.4, 43.2, 43.2);
circle3.setFilled(true);
circle3.setFillColor(Color.RED);
circle3.setColor(Color.RED);
add (circle3);
}
}
Assignment 5
import acm.graphics.*;
import acm.program.*;
import java.awt.*;
public class ProgramHierarchy extends GraphicsProgram {
public void run() {
double widht = 200;
double height = 80;
GRect rek1 = new GRect(getWidth()/2-widht/2, getHeight()/2-3*(height), widht, height);
add (rek1);
GRect rek2 = new GRect(getWidth()/2-widht/2, getHeight()/2-(height), widht, height);
add (rek2);
GRect rek3 = new GRect(getWidth()/2-2*widht, getHeight()/2-(height), widht, height);
add (rek3);
GRect rek4 = new GRect(getWidth()/2+widht, getHeight()/2-(height), widht, height);
add (rek4);
GLine linjeRek3TillRek1 = new GLine(( getWidth()/2)-(1.5*widht), (getHeight()/2)-(height), getWidth()/2, getHeight()/2-2*(height));
add (linjeRek3TillRek1);
GLine linjeRek2TillRek1 = new GLine( getWidth()/2, getHeight()/2-(height), getWidth()/2, getHeight()/2-2*(height));
add (linjeRek2TillRek1);
GLine linjeRek4TillRek1 = new GLine( getWidth()/2+(1.5*widht), getHeight()/2-(height), getWidth()/2, getHeight()/2-2*(height));
add (linjeRek4TillRek1);
GLabel label = new GLabel("Program");
label.setLocation(getWidth()/2-label.getWidth()/2, getHeight()/2-(2.5*(height)));
add(label);
GLabel label2 = new GLabel("GraphicsProgram");
label2.setLocation(getWidth()/2-label.getWidth(), getHeight()/2-(height/2));
add(label2);
GLabel label3 = new GLabel("ConsoleProgram");
label3.setLocation(getWidth()/2-1.5*widht-label.getWidth(), getHeight()/2-(height/2));
add(label3);
GLabel label4 = new GLabel("DialogProgram");
label4.setLocation(getWidth()/2+1.5*widht-label.getWidth(), getHeight()/2-(height/2));
add(label4);
}
}