Question

Write a simple Java program with the following naming structure: Open Eclipse Create a workspace called...

Write a simple Java program with the following naming structure:

  • Open Eclipse
  • Create a workspace called hw1
  • Create a project called hw1 (make sure you select the “Use project folder as root for sources and class files”)
  • Create a class called Hw1 in the hw1 package (make sure you check the box that auto creates the main method).
  • Add a comment to the main that includes your name

Write code that demonstrates the use of each of the following basic Java commands.

Before each line or block of code add a comment with the number of the task. For example, 1. below might look like this:

//1
System.out.print("this is the easiest homework assignment ever!");

  • System.out.print
  • System.out.println
  • Outputting a string literal
  • Using "\t" and "\n"
  • Using + to concatenate two things inside a print
  • Creating an int, double, boolean and char primitive variables
  • Storing values into the previous variables
  • Creating a String object
  • Storing a string into the String object
  • Print all of the previously created variables and the String object and separated by tabs
  • Show how to make a variable a constant
  • Create a Scanner object
  • Use the Scanner object to read from the keyboard an int, double, char and String and store the values into the previously declared variables. (char is difficult. Research / Google how to do this...)
  • Print the variables again, and separated by tabs
  • Use three of the Math class methods
  • Create a use a Random object to print a random number between 10 and 20 inclusively
  • Show an example of data type conversion without explicit casting
  • Show an example of data type conversion with casting
  • Show the use of an if statement comparing two primitives
  • Show the use of the .equals() method inside an if statement to compare two Strings
  • Show the use of an if-else statement
  • Use a For loop to print the numbers from 1 to 20 inclusively and all on the same line
  • Use a While loop to print the even numbers from 1 to 61 and all on the same line
  • Use a Do-While loop to prompt the user to enter an odd positive number
  • Create an int array that can hold 100 values. Fill it will random numbers between 0 and 300.
  • Find and print the smallest number in the int array.
  • Find and print the average of the numbers in the int array.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code -


import java.util.*;
public class Main
{
   public static void main(String[] args) {
   //1
   System.out.print("this is the easiest homework assignment ever!\n");
   //2 println
   System.out.println("This print the code and bring cursor to new line.");
   //3 \t and \n
   System.out.println("\tThis give a tab space and \n is used to print to new line");
   //4 + to concantenate two String
   System.out.print("Using +"+" to concantenate strings \n");
   //5 int double char
   int integerNum;
   double doubleNum;
   char character;
   //6 storing values
   integerNum = 5;
   doubleNum = 10.54;
   character = 'A';
   //7 String object
   String str1;
   //8 storing string in string object
   str1 = new String("New string created");
   //9 print all variables
   System.out.println(integerNum+"\t"+doubleNum+"\t"+character+"\t"+str1);
   //10contant variables using static final keyword
   final int a = 10;
   //11 Scanner object
   Scanner sc = new Scanner(System.in);
   //12 store value
   System.out.println("Enter int double char and string to store in variable");
   integerNum = sc.nextInt();
   doubleNum = sc.nextDouble();
   character = sc.next().charAt(0);
   str1 = sc.next();
   //13 print variable again
   System.out.println(integerNum+"\t"+doubleNum+"\t"+character+"\t"+str1);
   //14 Math class
   int min = Math.min(10, 20); //print 10
   double floor = Math.floor(7.343); // floor = 7.0
   double squareRoot = Math.sqrt(4); //squareRoot = 2
   //15Random number in range 20 to 30
   Random r = new Random();
       int rand = r.nextInt(11) + 10;
      
       //16Implicit casting or without explicit casting -
       int x = 10; // occupies 4 bytes
double y = x; // occupies 8 bytes
System.out.println(y); // prints 10.0
     
   //17Explicit casting
   double xx = 10.5;
int yy = (int) xx;//print 10
System.out.println(yy);
     
   //18if statement comparing primitive
   if(10>5){
   System.out.println("10 is greater than 5");
   }
  
   //19 equals
   String s1 = "John";
   if(s1.equals("John")){
   System.out.println("String are equal");
   }
  
   //20 if else
   if(10>20){
   System.out.println("10 is greater than 20");
   }
   else{
   System.out.println("10 is less than 20");
   }
  
   //21 for loop
   for(int i = 1; i <=20;i++){
   System.out.print(i+" ");
   }
  
   //22while loop
   int count = 1;
   while(count<=61){
   if(count%2==0){
   System.out.print(count+" ");
   }
   count++;
   }
  
   //23 do while
   int number;
   do{
   System.out.println("Enter an odd positive number");
   number = sc.nextInt();
   }while(number%2==0 || number<0);
  
   int arr[] = new int[100];
   for(int i = 0; i <arr.length;i++){
   arr[i] = r.nextInt(301);
   }
   //24smallest
   int smallest = 10000;
   for(int i = 0; i <arr.length;i++){
   if(smallest>arr[i]){
   smallest = arr[i];
   }
   }
   System.out.println("smallest is "+smallest);
  
   //25 average
   int average,total=0;
   for(int i = 0; i <arr.length;i++){
   total+=arr[i];
  
   }
   average = total / arr.length;
   System.out.println("Average is "+average);
  
   }
}

Screenshots-

pls do give a like thank you

Add a comment
Know the answer?
Add Answer to:
Write a simple Java program with the following naming structure: Open Eclipse Create a workspace called...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • PLEASE ANSWER #5AND #6, THE ANSWER FOR #3 AND #4 ARE ALREADY PROVIDED!!! 3 .Using Java,...

    PLEASE ANSWER #5AND #6, THE ANSWER FOR #3 AND #4 ARE ALREADY PROVIDED!!! 3 .Using Java, Write a computer program that prompts the user for one number, n for the number of items in the array to sort, and create and sort 1000 arrays of this size timing the run to get an average time to sort an array of this size. Then do the following: Initiate a variable running_time to 0 Create a for loop that iterates 1000 times....

  • JAVA HELP: Directions Write a program that will create an array of random numbers and output...

    JAVA HELP: Directions Write a program that will create an array of random numbers and output the values. Then output the values in the array backwards. Here is my code, I am having a problem with the second method. import java.util.Scanner; import java.util.Random; public class ArrayBackwards { public static void main(String[] args) { genrate(); print(); } public static void generate() { Scanner scanner = new Scanner(System.in);    System.out.println("Seed:"); int seed = scanner.nextInt();    System.out.println("Length"); int length = scanner.nextInt(); Random random...

  • In this same program I need to create a new method called “int findItem(String[] shoppingList, String...

    In this same program I need to create a new method called “int findItem(String[] shoppingList, String item)” that takes an array of strings that is a shopping list and a string for an item name and searches through the “shoppingList” array for find if the “item” exists. If it does exist print a confirmation and return the item index. If the item does not exist in the array print a failure message and return -1. import java.util.Scanner; public class ShoppingList...

  • 5.4 Java Create a program that generates SuperLotto lottery numbers. Create a class called SuperLottoPlus.java Create...

    5.4 Java Create a program that generates SuperLotto lottery numbers. Create a class called SuperLottoPlus.java Create a method called generateSuperLottoNumbers() that returns an array of 6 random SuperLotto lottery numbers. The first 5 numbers must be from the range 1 to 47 The 6th number (the MEGA) must be from 1 to 27. Create a method called printTicket() that takes an integer array as an parameter it will loop through the integer array and print out the data Display the...

  • Create a java class that user put two inputs and first input generate n length array...

    Create a java class that user put two inputs and first input generate n length array of randomly generated numbers. and the second input changes that number of multiples in the array into zero. for example, if the user puts 3 and 5 then it generates 34 60 10 and since the second input is 5 then the multiple of 5 eliminates so it generates 34 0 0 here is the main method that I'm going to use class Main...

  • PrintArray vi Create a class called PrintArray. This is the class that contains the main method....

    PrintArray vi Create a class called PrintArray. This is the class that contains the main method. Your program must print each of the elements of the array of ints called aa on a separate line (see examples). The method getArray (included in the starter code) reads integers from input and returns them in an array of ints. Use the following starter code: //for this program Arrays.toString(array) is forbidden import java.util.Scanner; public class PrintArray { static Scanner in = new Scanner(System.in);...

  • JAVA :The following are descriptions of classes that you will create. Think of these as service...

    JAVA :The following are descriptions of classes that you will create. Think of these as service providers. They provide a service to who ever want to use them. You will also write a TestClass with a main() method that fully exercises the classes that you create. To exercise a class, make some instances of the class, and call the methods of the class using these objects. Paste in the output from the test program that demonstrates your classes’ functionality. Testing...

  • Write a Java program in Eclipse that reads from a file, does some clean up, and...

    Write a Java program in Eclipse that reads from a file, does some clean up, and then writes the “cleaned” data to an output file. Create a class called FoodItem. This class should have the following: A field for the item’s description. A field for the item’s price. A field for the item’s expiration date. A constructor to initialize the item’s fields to specified values. Getters (Accessors) for each field. This class should implement the Comparable interface so that food...

  • ( Object array + input) Write a Java program to meet the following requirements: 1. Define...

    ( Object array + input) Write a Java program to meet the following requirements: 1. Define a class called Student which contains: 1.1 data fields: a. An integer data field contains student id b. Two String data fields named firstname and lastname c. A String data field contains student’s email address 1.2 methods: a. A no-arg constructor that will create a default student object. b. A constructor that creates a student with the specified student id, firstname, lastname and email_address...

  • Write a Java program that has a method called arrayAverage that accepts an arrary of numbers...

    Write a Java program that has a method called arrayAverage that accepts an arrary of numbers as an argument and returns the average of the numbers in that array. Create an array to test the code with and call the method from main to print the average to the screen. The array size will be from a user's input (use Scanner). - array size = int - avergage, temperature = double - only method is arrayAverage Output:

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT