Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
JAVA Midterm Review Spring 2015 Daniel Estrada-001040277 1. Create a Java program that uses an inputbox to ask user to enter his/her name, the program displays his/her name on a message box. import javax.swing.JOptionPane; public class MidTermProgram1 { public static void main(String[] args) { String full_name; full_name = JOptionPane.showInputDialog("What is your name?" ,"full_name"); JOptionPane.showMessageDialog(null, full_name); } } 2. Create a Java program that can draw an Olympic ring , your name in red with font size 18. a. Olympic ring import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.Graphics2D; import javax.swing.JButton; import javax.swing.JComponent; import javax.swing.JTextArea; public class Number2olympicringsandname extends JComponent{ public void paint(Graphics g) { //draw name g.setFont(new Font("TimesRoman", Font.PLAIN, 16)); g.setColor(Color.BLACK); g.drawString("Daniel Estrada", 30, 90); Graphics2D g2 = (Graphics2D) g; g.drawOval (1,0,40,40); g.setColor(Color.YELLOW); g.drawOval (21,20,40,40); g.setColor(Color.BLACK); g.drawOval (41,0,40,40); g.setColor(Color.GREEN); g.drawOval (61,20,40,40); g.setColor(Color.RED); g.drawOval (81,0,40,40); g.setColor(Color.BLUE); } } b. view file (main file) import javax.swing.JFrame; public class Number2viewer { public static void main(String[] args) { JFrame frame = new JFrame(); frame.setSize(300, 300); frame.setTitle("Daniel Estrada"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLO SE); Number2olympicringsandname ft = new Number2olympicringsandname(); frame.add(ft); frame.setVisible(true); } } 3. Create a Java program that can draw 4 cars in any arrangement. a. Car.java import java.awt.Graphics2D; import java.awt.Rectangle; import java.awt.geom.Ellipse2D; import java.awt.geom.Line2D; import java.awt.geom.Point2D; /** A car shape that can be positioned anywhere on the screen. */ public class Cars { private int xLeft; private int yTop; /** Constructs a car with a given top left corner. @param x the x-coordinate of the top-left corner @param y the y-coordinate of the top-left corner */ public Cars(int x, int y) { xLeft = x; yTop = y; } /** Draws the car. @param g2 the graphics context */ public void draw(Graphics2D g2) { Rectangle body = new Rectangle(xLeft, yTop + 10, 60, 10); Ellipse2D.Double frontTire = new Ellipse2D.Double(xLeft + 10, yTop + 20, 10, 10); Ellipse2D.Double rearTire = new Ellipse2D.Double(xLeft + 40, yTop + 20, 10, 10); // The bottom of the front windshield Point2D.Double r1 = new Point2D.Double(xLeft + 10, yTop + 10); // The front of the roof Point2D.Double r2 = new Point2D.Double(xLeft + 20, yTop); // The rear of the roof Point2D.Double r3 = new Point2D.Double(xLeft + 40, yTop); // The bottom of the rear windshield Point2D.Double r4 = new Point2D.Double(xLeft + 50, yTop + 10); Line2D.Double frontWindshield = new Line2D.Double(r1, r2); Line2D.Double roofTop = new Line2D.Double(r2, r3); Line2D.Double rearWindshield = new Line2D.Double(r3, r4); g2.draw(body); g2.draw(frontTire); g2.draw(rearTire); g2.draw(frontWindshield); g2.draw(roofTop); g2.draw(rearWindshield); } } b. CarComponent.java import java.awt.Graphics; import java.awt.Graphics2D; import javax.swing.JComponent; /** This component draws two car shapes. */ public class CarComponent extends JComponent { public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; Cars car1 = new Cars (0, 0); int x = getWidth() - 60; int y = getHeight() - 30; Cars car2 = new Cars (x, y); int a = getWidth() - 100; int b = getHeight() - 100; Cars car3 = new Cars (a,b); int q = getWidth() - 70; int z = getHeight() - 70; Cars car4 = new Cars (q,z); car1.draw(g2); car2.draw(g2); car3.draw(g2); car4.draw(g2); } } c. CarView.java import javax.swing.JComponent; import javax.swing.JFrame; public class Cars_4_Viewer extends JComponent { public static void main(String[] args) { JFrame frame = new JFrame(); frame.setSize(300, 400); frame.setTitle("Four cars"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CL OSE); CarComponent r = new CarComponent(); frame.add(r); frame.setVisible(true); } } 4. Create a Java program that can draw an Alien face. ( must be different from the textbook version) a. FaceComponent //Daniel Estrada import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.geom.Ellipse2D; import java.awt.geom.Ellipse2D.Double; import javax.swing.JComponent; public class FaceComponent extends JComponent { public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D)g; Ellipse2D.Double head = new Ellipse2D.Double(5.0D, 10.0D, 100.0D, 200.0D); g2.setColor(Color.GREEN); g2.fill(head); g2.setColor(Color.BLACK); g2.draw(head); Ellipse2D.Double rightEye = new Ellipse2D.Double(75.0D, 70.0D, 15.0D, 30.0D); g2.setColor(Color.RED); g2.fill(rightEye); g2.setColor(Color.BLACK); g2.draw(rightEye); Ellipse2D.Double leftEye = new Ellipse2D.Double(25.0D, 70.0D, 15.0D, 30.0D); g2.setColor(Color.RED); g2.fill(leftEye); g2.setColor(Color.BLACK); g2.draw(leftEye); Ellipse2D.Double mouth = new Ellipse2D.Double(44.0D, 120.0D, 40.0D, 22.0D); g2.setColor(Color.RED); g2.fill(mouth); g2.setColor(Color.BLACK); g2.draw(mouth); g2.setColor(Color.BLACK); Ellipse2D.Double rightnose = new Ellipse2D.Double(76.0D, 83.0D, 10.0D, 10.0D); g2.fill(rightnose); g2.setColor(Color.BLACK); Ellipse2D.Double leftnose = new Ellipse2D.Double(27.0D, 83.0D, 10.0D, 10.0D); g2.fill(leftnose); } } b. FaceViewer import javax.swing.JFrame; public class AlienFaceViewer { public static void main(String[] args) { JFrame frame = new JFrame(); frame.setSize(400, 500); frame.setTitle("Alien Face!"); frame.setDefaultCloseOperation(3); FaceComponent component = new FaceComponent(); frame.add(component); frame.setVisible(true); } } 5. Create a Java program that prompts the user to enter 2 numbers, the program calculate and print The sum The different The average package midterm_review; import java.util.Scanner; public class MidtermReview_5 { public static void main(String[] args) { double num1, num2; Scanner input = new Scanner(System.in); System.out.println("Please select your first number\n"); num1 = input.nextDouble(); System.out.println("Please select your second number\n"); num2 = input.nextDouble(); //Sum of two numbers System.out.println("The sum of two numbers is " + (num1 + num2)); //Difference of two numbers System.out.println("The difference of two numbers is " + (num1 - num2)); //Average of two numbers System.out.println("The average of two numbers is " + (num1 + num2)/2); } } 6. Create a Java program that prompts the user for a lengths and width of a rectangle The area and the perimeter of the rectangle The length of the diagonal ( use Pythagorean theorem) package defaultpackage; import java.util.Scanner; public class midterm_6 { public static void main(String[] args) { double length, width = 0; Scanner in = new Scanner(System.in); System.out.println("Please enter the rectangle length"); length = in.nextDouble(); System.out.println("please enter the rectangle width"); width = in.nextDouble(); double perimeter = (length + width) * 2; double rectPythag = Math.pow(width, 2) + Math.pow(length, 2); double rectangleDiagonal = Math.sqrt(rectPythag); System.out.println("Rectangle perimeter is "+perimeter); System.out.println("Rectangle diagonal is " + rectangleDiagonal); } } 7. Create an Android Application that write your name and post your photo. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="${packageName}.${activityClass}" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="32dp" android:text="Group Number 7" android:textSize="35dp" /> <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:src="@drawable/picture" /> 8. Create an Android application that includes 3 pages. The first pages includes 2 buttons , The first button display "My favorite Movie", which links to another page, displays a picture of your movie. The second button displays "My favorite book", which links to another page, displays a picture of your favorite book. a. Main.java package com.example.favorite; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class Main extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button b = (Button) findViewById(R.id.bookbutton); Button b2 = (Button) findViewById(R.id.moviebutton); b.setOnClickListener(new OnClickListener() { public void onClick(View v) { startActivity(new Intent(Main.this, Book.class)); } }); b2.setOnClickListener(new OnClickListener() { public void onClick(View v) { startActivity(new Intent(Main.this, Movie.class)); } }); } } b. Book.java package com.example.favorite; import android.app.Activity; import android.os.Bundle; public class Book extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.book); } } c. Movie.java package com.example.favorite; import android.app.Activity; import android.os.Bundle; public class Movie extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.movie); } }