Download Track2-Exercise1

yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Track 2: Java Basics
Exercise 1: Writing Java Codes 1
Instruction: This is a hands-on seatwork. Follow the
instructions given below.
Hours: 3.5 hours
Name your project as <FamilyName>Ex1
A. Correct the following codes by fixing some bugs and applying the Java coding standards. Drop all your
corrected classes inside
Code 1
public class Oops {
public static void main(String[] args) {
int a = 7, b = 42;
minimum(a, b);
if {smaller = a} {
System.out.println("a is the smallest!");
public static void minimum(int a, int b) {
// returns which int is smaller
if (a < b) {
int smaller = a;
} else (a => b) {
int smaller = b;
return int smaller;
Code 2
public class LotsOf Errors {
public static main(String args) {
System.println("Hello, world!);
public static void message {
System.out println("This program surely cannot";
System.out.println("have any so-called "errors" in it");
Code 3
public class Temperature {
public static void main(String[] args) {
double tempf = 98.6;
double tempc = 0.0;
ftoc(tempf, tempc);
System.out.println("Body temp in C is: " + tempc);
// Converts Fahrenheit temperatures to Celsius.
public static void ftoc(double tempf, double tempc) {
tempc = (tempf - 32) * 5 / 9;
Code 4
public class Variables {
public static void main(String[] args){
int x, y, z =3;
y = x;
z = y;
Code 5
Public class MyClass{
public static void main( String[] args ){
int x = 1; y = 2;
Code 5
class VBUGGY {
public static main (string param[]){
string name;
// int value;
output.out = new(output);
out.writeln("Hello. Please type your name:");
name = input.readln();
out.writeln("Now type a whole number:);
value = input.readint();
out.writeln("The square of your number is " + x*x);
B. The following program is legal under Java's syntax rules, but it is difficult to read because of its layout and lack
of comments. Reformat it using the rules given in this chapter, and add a comment header at the top of the
program. Drop the class inside
public class GiveAdvice{ public static
void main(String[ ]args){ System.out.println (
"Programs can be easy or difficult"); System.out.println(
"to read, depending upon their format."
); System.out.println()
; System.out.println("Everyone, including yourself, will
"happier if you choose to format your");System.out.println("Programs."); }
C. Write a complete Java class WellFormed that prints the following exact output. Drop the class inside Drop the class inside
A well-formed Java program has a main
method with { and } braces.
A System.out.println statement has ( and )
and usually a String that starts and ends
with a " character. (But we type \" instead!)
D. Identify which of the identifiers are valid in Java in terms of its syntax and best practices. Then, create a class
JavaID and declare properly those you selected using the correct data type. No main() method is needed. Drop
the class inside
E. Now, create a class JavaDeclaration that will store the following data values into their appropriate
identifiers with the correct data types. No main() method is needed. Drop the class inside
Write a Java class AssignMe that has an entrypoint main() which performs the following steps:
a) Declare two int variables named x and y.
b) Assign 3 to x.
Assign twice the value of x to y.
d) Interchange the value of x and y (without explicitly assign 3 to y).
e) Print the values of both variables on screen.
Drop the class inside
G. Write a Java class DeclareMe that has an entrypoint main() which performs the following steps:
a) Declare a float variable called f.
b) Declare an int variable called k.
Assign 22 to k.
d) Assign the value of k to f.
e) Print the values of both variables on screen.
Drop the class inside
H. Write a default class Product to maintain information about goods of a certain product that are stored
in a warehouse. Each object of type Product is characterized by the name of the product (fixed the
moment the product is created) and by the number of pieces of the product that are stored in the
warehouse (initially 0). Implement the methods downLoad (to increment the number of stored pieces),
upLoad (to reduce the number of stored pieces), and print() to return the information about a product
(e.g., "Okinawa lamp, 25 pieces"). Then create a public class TestProduct that will instantiate, initialize
the variables and execute the methods of the Product class. A sample execution is shown below:
public class TestProduct {
public static void main (String[] args) {
Product lamp = new Product(); // instantiate
lamp.var1 = ‘IKEA Lamp’; // replace the variable name
lamp.var2 = 10000; // replace the variable name
System.out.println("Before the loading: " + lamp.print());
System.out.println("After the loading: " + lamp.print());
Drop all the classes inside
After debugging this program identify approximately 8 compiler errors, 3 logic errors, 3 user-experience
errors and 1 readability error. Write down all these errors below the code using block comment /* */.
public class FahrenheitToCelsiusx{
public static void main( String [] args ) {
// declare and initialize data
float Celsius = 35.5;
float Fahrenheit = 22.6;
// calculate celsius equivalent
double celsius = 5/9 * fahrenheit - 32;
// output results
System.out.println( fahrenheit, "\U00B0F = " );
System.out.println( celcius, "u00B0C" );
Drop all the classes inside
Copy protected with