Write a simple Java program with the following naming structure:
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!");
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
Write a simple Java program with the following naming structure: Open Eclipse Create a workspace called...
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 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 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 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 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. 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 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 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 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 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: