Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java code as well as the runtime and space requirements using Big O 1. /* senten

ID: 3584993 • Letter: J

Question

Java code as well as the runtime and space requirements using Big O

1. /* sentence should contain the same words but in reverse order when this function returns. You may assume the sentence contains only a single punctuation make at the end and you should leave it there. You may ignore capitalization. For example: "I am a house." should return "house a am I." For an extra challenge, try it using O(1) space. */

static void reverse(char[] sentence) { }

2. /* +++Return the smallest value that exists in both arrays. Brute force. */

static int minShared(int[] array1, int[] array2) { }

3. /* Return the smallest value that exists in both arrays. Optimize for speed. Must be better than n^2 or m^2 where n and m are the lengths of the arrays. */

static int minShared(int[] array1, int[] array2) { }

4. Provide function definitions sufficient to solve the problem and tests for those functions, but DO NOT write the code that actually implements the functions.

This program should read in a 4x4 matrix from a user in the format of a series of numbers separated by spaces, with the first row comprising the first 4 numbers, the second row the next 4, and so on. The user then has the option to Rotate or Reflect the matrix. If the user chooses to rotate, they should be able to enter ANY integer and the program should print out the result of printing the matrix rotated that many times, where a matrix rotated 1 time is equivalent to a 90 degree rotation, 2 times a 180, 3 times 270, 4 times 360, etc. If the user chooses to Reflect, they should be able to reflect across either the vertical midline or the horizontal midline. The program should print out the result of this reflect.

(REMEMBER: Define the functions with empty { } and the code to test them. For the tests, you don't have to use valid Java to construct the input - as long as it's clear what the input should be from whatever you do write.)

Explanation / Answer

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

import java.util.Scanner;


public class Functions {

/**
* @param args the command line arguments
*/
  
static int minShared(int [] array1,int [] array2)//better than n^2 and m^2
{
int i=0,max=array1[0];
while(i<array1.length)
{
if(max<array1[i])max = array1[i];//finding max of array1
i++;
}
i=0;
while(i<array2.length)
{
if(max<array2[i])max = array2[i];//finding max of array2 nad array1
i++;
}
  
int a[] = new int[max+1];//creating new array with max
int b[] = new int[max+1];//creating new array with max
  
i=0;
while(i<array1.length)//finding frequency...
{
a[array1[i]]++;
i++;
}
i=0;
while(i<array2.length)//finding frequency...
{
b[array2[i]]++;
i++;
}
i=0;
while(i<=max)//
{
if(a[i]!=0 && b[i]!=0)break;//finding min
i++;
}
return i;//returning min
}
  
static void reverse(char [] sentence)
{
int n = sentence.length;//find length of the string...
if(sentence[n-1]=='.' && n!=0)
n--;//if last char is .
int i=0;
char c;//space O(1)
  
n--;
  
while(i<n)//reversing sentence
{
//swapping values
c = sentence[i];
sentence[i]=sentence[n];
sentence[n]=c;
i++;
n--;
}
  
n=0;
i=0;
int k;
while(i<sentence.length)
{
while(sentence[n]!=' ' && n< sentence.length)//finding words..
{ n++;
if(n==sentence.length)break;
}
  
k=n-1;
if(n==sentence.length)break;
while(i<k)
{//swapping values
c = sentence[i];
sentence[i]=sentence[k];
sentence[k]=c;
i++;
k--;
}
n++;
i=n;
}
}
  
public static void main(String[] args) {
  
  
  
Scanner sc = new Scanner(System.in);// to read input
  
System.out.print("Enter sentence to reverse :");
String s;
s = sc.nextLine();//reading sentence
char [] sentence = new char[s.length()];//to read sentence
  
int i=0;
while(i<s.length())
{
sentence[i]=s.charAt(i);
i++;
}
  
  
  
reverse(sentence);//calling reverse funtion
  
System.out.println("After sentence reversing : ");
i=0;
while(i<s.length())
{
System.out.print(sentence[i]);
i++;
}
  
System.out.println();
  
  
  
int a[] = {1,2,3,4,5,6};
int b[] = {3,6,7,8};
System.out.println("Min value : "+minShared(a,b));
  
  
  
  
  
}
}

output:-

run:
Enter sentence to reverse :i am a house.
After sentence reversing :

house a am i.
Min value : 3
BUILD SUCCESSFUL (total time: 4 seconds)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote