Modifying programs is required from time to time. This lesson will take an exist
ID: 3883695 • Letter: M
Question
Modifying programs is required from time to time. This lesson will take an existing program and modify it to make updates to the code. Modify last week’s code to implement user input into the program and process the information. Web pages implement Java Applets that are security signed. In this lesson, a basic Applet that shows shapes will be created and tested in the Applet Viewer. Assignment: Use last week’s assignment and modify it to enter the name and age from the Console (Console – see example 3.9 on page 129) or Dialog Box(Dialog box - For String variable see Example 3.19 on page 151. For Integer input see Example 3.20 on page 152-153). Create an Applet that uses two integer (int) variables x and y to place the picture of four circles (See Example 4.6 page 185) on the screen. Alternate the colors by setting the g.setColor method (see Example 4.9 on page 193). Also, use the g.drawString to place your name above the circles.
Here is code I put in for week 1:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package myfirstprogram;
/**
*
* @author USER
*/
public class MyFirstProgram {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
//This is my first program
// Enter your name where is says Your Name
String name = "Matt Fitzgerald"; //See table 2.7 for data types
// Enter your age or the age you would want to be
int age = 22;
int ageNextBirthday, ageLastBirthday;
// age-- subtracts 1 from age
ageNextBirthday = age-- ; //See table 2.12 for Shortcut Operators
// age++ adds 1 to age
ageLastBirthday = age++ ;
// Printout your results
System.out.println("Hello, my name is "+name+".");
System.out.println("My age is "+age+".");
System.out.println("My age last birthday was "+ageLastBirthday+".");
System.out.println("My age next birthday is "+ageNextBirthday+".");
}
}
Explanation / Answer
package myfirstprogram;
// import Applet class from applet package
import java.applet.Applet;
// Read data from input stream using Scanner class present in java.util package
import java.util.Scanner;
// Use Graphics class present in java.awt package
import java.awt.Graphics;
//Use Color class for applying colors present in java.awt package
import java.awt.Color;
public class MyFirstProgram extends Applet
{
int age;
String name;
int x,y;
// init() is overriden from Applet class which initializes Applet
public void init()
{
Scanner obj=new Scanner(System.in);
System.out.println("Enter name");
name=obj.nextLine();
System.out.println("Enter age");
age=obj.nextInt();
x=100;
y=100;
}
// to write something on the Applet use paint() passing Graphics reference as parameter
public void paint(Graphics g)
{
g.setColor(Color.red); // setColor() sets the particular color for object where Color is class and red is constant
// draw the circle using fillOval() of Graphics class which takes x,y,width and height as parameters
g.fillOval(x,y,100,100);
// write text on Applet using drawString() which takes text, x and y axis
g.drawString(name,x+30,90);
g.setColor(Color.yellow);
x=x+100;
g.fillOval(x,y,100,100);
g.drawString(name,x+30,90);
g.setColor(Color.blue);
x=x+100;
g.fillOval(x,y,100,100);
g.drawString(name,x+30,90);
g.setColor(Color.green);
x=x+100;
g.fillOval(x,y,100,100);
g.drawString(name,x+30,90);
}
}
// Load the applet
/* <applet code="MyFirstProgram" width="500" height="500"></applet */
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.