Use last week’s assignment and modify it to enter the name and age from the Cons
ID: 3594530 • Letter: U
Question
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 the code from last week that needs to be modified. (Name - Stacy Smith)
/*
* 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 Stacy
*/
public class MyFirstProgram {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
//Write a program to calculate birthday age
// Enter your name where it says Your Name
String name = "Stacy";
// Enter your age or desired age
int age = 43;
int ageNextBirthday, ageLastBirthday;
// age-- subtracts 1 from age
ageNextBirthday = age-- ;
// 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
1) MyFirstProgram.java
package myfirstprogram;
import java.util.Scanner;
/**
*
* @author Stacy
*/
public class MyFirstProgram {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
//Initializing Scanner class to read the input from console at runtime
Scanner sc=new Scanner(System.in);
//This statement is for asking the user for entering name
System.out.println("Please enter name");
String name = sc.nextLine();//capturing user entered data in to name variable
//This statement is for asking the user for entering age
System.out.println("Please enter age");//capturing user entered data in to age variable
int age = sc.nextInt();
int ageNextBirthday, ageLastBirthday;
// age-- subtracts 1 from age
ageNextBirthday = age-- ;
// 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+",") ;
//closing Scanner object at the end
sc.close();
}
}
2) Circle.java (Applet Program)
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.util.Scanner;
/*<applet code="ConcCircles" height=250 width=300>
</applet>
*/
public class Circle extends Applet
{
public void paint(Graphics g)
{
int xCor,yCor;
Scanner sc=new Scanner(System.in);
System.out.println("Please enter X- co ordinate value");
xCor=sc.nextInt();
System.out.println("Please enter Y- co ordinate value");
yCor=sc.nextInt();
g.setColor(Color.yellow);
g.fillOval(xCor, yCor, 200, 200);
g.setColor(Color.red);
g.fillOval(xCor+25, yCor+25, 150, 150);
g.setColor(Color.BLUE);
g.fillOval(xCor+50, yCor+50, 100, 100);
g.setColor(Color.GREEN);
g.fillOval(xCor+75, yCor+75, 50, 50);
g.setColor(Color.red);
g.drawString("MY NAME IS <<NAME<<", 40, 125);
sc.close();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.