JAVA Language
public class ArraySkills { public static void main(String[] args) { // *********************** // For each item below you must code the solution. You may not use any of the // methods found in the Arrays class or the Collections classes // String[] myData; // 1. Instantiate the given array to hold 10 Strings. // 2. Add your name to the Array at index 0 and a friend's name to the Array at index 4 // 3. Move your friend's name to index 0 in the array and "delete" their name from index 4 // 4. Fill up all of the remaining indeces in the array with more names // 5. Swap the values at index 5 and index 1 // 6. Print the array from beginning to end. // 7. Shuffle the array of strings // 8. Find and print the longest and shortest Strings in the array // 9. Add up the total number of characters in all of the strings in the array and print the answer // 10. Prompt the user for a String and then perform a linear search of the array // to see if that string is or is not in the array. Print if it is or is not found. // 11. Remove the item at index 4 of the array by shifting everything after it one spot to the left. // This means your array should have one empty index at the end after the shift (delete the duplicate item at the end). // 12. Create a new array that is twice as big as the current array and copy all of the items to the new array. // When complete, swap references so our old array gets garbage collected and the new array is pointed to by your array variable myData. // 13. Prompt the user to enter 2 numbers within the range of the array's length. If the first is larger than the second print backwards from that index to the first. // If the second is larger than the first, print forward in the array from the first index to the second. } }
Hi, Please find my implementation.
Please let me know in case of any issue.
import java.util.Random;
import java.util.Scanner;
public class ArraySkills {
public static void printArray(String[] arr){
for(int i=0; i<arr.length; i++)
System.out.println(arr[i]);
}
// Implementing Fisher–Yates shuffle
public static void shuffleArray(String[] ar)
{
Random rnd = new Random();
for (int i = ar.length - 1; i > 0; i--)
{
int index = rnd.nextInt(i + 1);
// Simple swap
String a = ar[index];
ar[index] = ar[i];
ar[i] = a;
}
}
public static void main(String[] args) {
// ***********************
// For each item below you must code the solution. You may not use any of the
// methods found in the Arrays class or the Collections classes
//
String[] myData;
Scanner sc = new Scanner(System.in);
// 1. Instantiate the given array to hold 10 Strings.
myData = new String[10];
// 2. Add your name to the Array at index 0 and a friend's name to the Array at index 4
myData[0] = "Pravesh Kumar";
myData[4] = "Alex Bob";
// 3. Move your friend's name to index 0 in the array and "delete" their name from index 4
myData[0] = myData[4];
myData[4] = null;
//System.out.println(myData[0]);
// 4. Fill up all of the remaining indexes in the array with more names
for(int i=1; i<10; i++){
System.out.print("Enter name to store at index "+i+" :" );
myData[i] = sc.nextLine();
}
// 5. Swap the values at index 5 and index 1
String temp = myData[5];
myData[5] = myData[1];
myData[1] = temp;
// 6. Print the array from beginning to end.
printArray(myData);
// 7. Shuffle the array of strings
shuffleArray(myData);
// 8. Find and print the longest and shortest Strings in the array
// initializing with first element
String longest = myData[0];
String shortest = myData[0];
for(int i=1; i<myData.length; i++){
if(myData[i].length() < shortest.length())
shortest = myData[i];
if(myData[i].length() > longest.length())
longest = myData[i];
}
System.out.println("Longest string: "+longest);
System.out.println("Shortest string: "+shortest);
// 9. Add up the total number of characters in all of the strings in the array and print the answer
int total = 0;
for(int i=0; i<myData.length; i++)
total += myData[i].length();
System.out.println("Total number of characters: "+total);
// 10. Prompt the user for a String and then perform a linear search of the array
// to see if that string is or is not in the array. Print if it is or is not found.
System.out.print("Enter a string to search: ");
String search = sc.nextLine();
int i;
for(i=0; i<myData.length; i++){
if(search.equals(myData[i])){
System.out.println(search+ " found at index "+i);
break;
}
}
if(i == myData.length){
System.out.println(search+ " not found");
}
// 11. Remove the item at index 4 of the array by shifting everything after it one spot to the left.
// This means your array should have one empty index at the end after the shift (delete the duplicate item at the end).
for(int j=4; j<myData.length-1; j++){
myData[j] = myData[j+1];
}
myData[myData.length-1] = null;
// 12. Create a new array that is twice as big as the current array and copy all of the items to the new array.
// When complete, swap references so our old array gets garbage collected and the new array is pointed to by your array variable myData.
String[] newArr = new String[myData.length*2];
for(int j=0; j<myData.length; j++){
newArr[j] = myData[j];
}
myData = newArr;
newArr = null;
// 13. Prompt the user to enter 2 numbers within the range of the array's length. If the first is larger than the second print backwards from that index to the first.
// If the second is larger than the first, print forward in the array from the first index to the second.
int low, high;
System.out.print("Enter lower index: ");
low = sc.nextInt();
System.out.print("Enter higher index: ");
high = sc.nextInt();
if(low <= high){
for(int j=low; j<=high; j++)
System.out.println(myData[j]);
}else{
for(int j=high; j>=low; j--)
System.out.println(myData[j]);
}
}
}
/*
Sample run:
Enter name to store at index 1 :pravesh
Enter name to store at index 2 :alex
Enter name to store at index 3 :bob
Enter name to store at index 4 :mukesh
Enter name to store at index 5 :ramesh
Enter name to store at index 6 :vikash
Enter name to store at index 7 :pintu
Enter name to store at index 8 :ajad
Enter name to store at index 9 :gaurav
Alex Bob
ramesh
alex
bob
mukesh
pravesh
vikash
pintu
ajad
gaurav
Longest string: Alex Bob
Shortest string: bob
Total number of characters: 55
Enter a string to search: ajad
ajad found at index 9
Enter lower index: 2
Enter higher index: 7
pravesh
mukesh
alex
ramesh
bob
vikash
*/
JAVA Language public class ArraySkills { public static void main(String[] args) { // *********************** // For...
[JAVA] help —————— ListArrayMain.java ——————- public class ListArrayMain { public static void main (String[] args) { ListArray L1 = new ListArray(); // constructs the list L1 L1.InsertBegin(2); L1.InsertBegin(3); L1.InsertBegin(7); L1.InsertBegin(15); L1.InsertBegin(5); L1.InsertEnd(666); L1.InsertAfter(1000,7); // Now, let's print out the array to verify the insertions worked. System.out.println("The items in the list are:"); for (int i = 0; i<= L1.lastCell; i++) { System.out.println(L1.a[i]); } System.out.println("The last cell number is: " + L1.lastCell); }// end main } ———————————- ListArray.java ———————————— public class ListArray...
This is for a java program public class Calculation { public static void main(String[] args) { int num; // the number to calculate the sum of squares double x; // the variable of height double v; // the variable of velocity double t; // the variable of time System.out.println("**************************"); System.out.println(" Task 1: Sum of Squares"); System.out.println("**************************"); //Step 1: Create a Scanner object //Task 1. Write your code here //Print the prompt and ask the user to enter an...
Priority Queue Demo import java.util.*; public class PriorityQueueDemo { public static void main(String[] args) { //TODO 1: Create a priority queue of Strings and assign it to variable queue1 //TODO 2: Add Oklahoma, Indiana, Georgia, Texas to queue1 System.out.println("Priority queue using Comparable:"); //TODO 3: remove from queue1 all the strings one by one // with the "smaller" strings (higher priority) ahead of "bigger"...
Explain this java code, please. import java.util.Scanner; public class Program11 { public static void main(String[] args) { Scanner stdIn = new Scanner(System.in); final int maxSize = 128; String[] titles = new String[maxSize]; int[] lengths = new int[maxSize]; int numDVDs = 0; String op; op = menu(stdIn); System.out.println(); while (!op.equalsIgnoreCase("q")) { if (op.equalsIgnoreCase("a")) { if (numDVDs < maxSize) numDVDs = addDVD(titles, lengths, numDVDs, stdIn); } else if (op.equalsIgnoreCase("t")) searchByTitle(titles, lengths, numDVDs, stdIn); else if (op.equalsIgnoreCase("l")) searchByLength(titles, lengths, numDVDs, stdIn); System.out.println('\n');...
import java.util.Scanner; public class MPGMain { public static void main(String[] args) { Scanner input = new Scanner(System.in); Mileage mileage = new Mileage(); System.out.println("Enter your miles: "); mileage.setMiles(input.nextDouble()); System.out.println("Enter your gallons: "); mileage.setGallons(input.nextDouble()); System.out.printf("MPG : %.2f",mileage.getMPG()); } } public class Mileage { private double miles; private double gallons; public double getMiles()...
import java.util.LinkedList; public class testprintOut { private static LinkedList[] array; public static void main(String[] args) { int nelems = 5; array = new LinkedList[nelems]; for (int i = 0; i < nelems; i++) { array[i] = new LinkedList<String>(); } array[0]=["ab"]; System.out.println(array[0]); } } //I want to create array of linked lists so how do I create them and print them out efficiently? //Syntax error on token "=", Expression expected after this token Also, is this how I can put them...
Given main(). define the Artist class (in file Artist java) with constructors to initialize an artist's information, get methods, and a printlnfo() method. The default constructor should initialize the artist's name to "None' and the years of birth and death to 0. printinfo() should display Artist Name, born XXXX if the year of death is -1 or Artist Name (XXXX-YYYY) otherwise. Define the Artwork class (in file Artwork.java) with constructors to initialize an artwork's information, get methods, and a printinfo() method. The constructor should...
Convert into pseudo-code for below code =============================== class Main { public static void main(String args[]) { Scanner s=new Scanner(System.in); ScoresCircularDoubleLL score=new ScoresCircularDoubleLL(); while(true) { System.out.println("1--->Enter a number\n-1--->exit"); System.out.print("Enter your choice:"); int choice=s.nextInt(); if(choice!=-1) { System.out.print("Enter the score:"); int number=s.nextInt(); GameEntry entry=new GameEntry(number); ...
Need Help....Java MyProgram.java: public class MyProgram { public static void main(String[] args) { } } House.java: public class House { private String address; private double cost; private double interst; private int mortgageTime; private double monthPayment; } As you consider your life before you, one thing you may consider is buying a house in the future. In this assignment, you will explore concepts of this amazing opportunity and create a program that may be of benefit to you in the future!...
Java Here is the template public class ShiftNumbers { public static void main(String[] args) { // TODO: Declare matrix shell size // TODO: Create first row // TODO: Generate remaining rows // TODO: Display matrix } /** * firstRow * * This will generate the first row of the matrix, given the size n. The * elements of this row will be the values from 1 to n * * @param size int Desired size of the array * @return...