Exercise 3

View previous topic View next topic Go down

Re: Exercise 3

Post by Ivi on Sun Mar 20, 2016 4:14 pm

Assignment 3 Solution

Code:
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication3;

/**
 *
 * @author Ivi
 */
public class JavaApplication3 {
   
//First place
       
    public static int first(int[] place) {
     
        int list = 0;
       
        if(place.length > 1) {
           
            for(int x = 1; x <place.length;x++) {
               
                if(place[list] > place[x]){
                   
                    list = x;
         
                }               
            }
           
        }
           
        else {
                   
            System.out.print("Insert more than two times");
           
                    }
           
            return list;
        }
   
    //Second Place
    public static int second(int[] place) {
       
    int fastest = first(place);
    int secondfastest = 0;
   
    if (place.length > 1) {
       
        for(int y = 1;y < place.length;y++){
       
            if(place[fastest] < place[y]) {
           
                if(place[secondfastest] > place[y])
                   
                    secondfastest = y;
        }
        }
        }
     
    return secondfastest;
   
    }
   
    // Last Place
    public static int last(int[] place) {
     
        int list = 0;
       
        if(place.length > 1) {
           
            for(int z = 1; z <place.length;z++) {
               
                if(place[list] < place[z]){
                    list = z;
         
                }               
            }
           
        }               
            return list;
        }
   
   
  //Main Function
   
    public static void main(String[] arguments) {
       
        int[] place = { 341, 273, 278, 329 };
       
        String[] runner = { "Elena", "Thomas", "Suzie", "Phil" };
       
        System.out.println("Fastest time: " + place[first(place)] + " minutes - First place:: " + runner[first(place)]);
        System.out.println("Second fastest: " + place[second(place)] + " minutes - Second place: " + runner[second(place)]);
        System.out.println("Slowest: " + place[last(place)] + " - minutes - Last place: " + runner[last(place)]);
       
    }
   
}

Output

Code:

Fastest time: 273 minutes - First place:: Thomas
Second fastest: 278 minutes - Second place: Suzie
Slowest: 341 - minutes - Last place: Elena

avatar
Ivi
Admin

Posts : 41
Join date : 2016-02-06

View user profile http://website-lab.forum-board.net

Back to top Go down

Re: Exercise 3

Post by Ivi on Sun Mar 20, 2016 4:34 pm

Assignment 4 Part 1 - Book

Code:
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication3;

/**
 *
 * @author Ivi
 */
public class JavaApplication3 {
    
String title;
boolean borrowed;
// Creates a new Book
public JavaApplication3(String bookTitle) {

    title = bookTitle;
    borrowed = false;
    
}
// Marks the book as rented
public void borrowed() {
    
    borrowed = true;
}
// Marks the book as not rented
public void returned()
{
    borrowed = false;
}
// Returns true if the book is rented, false otherwise
public boolean isBorrowed() {
    
    if(borrowed){
        return true;}
        else{
        return false;}
}
// Returns the title of the book
public String getTitle() {
       return title;
}
public static void main(String[] arguments) {
// Small test of the Book class
JavaApplication3 example = new JavaApplication3("The Da Vinci Code");
System.out.println("Title (should be The Da Vinci Code): " + example.getTitle());
System.out.println("Borrowed? (should be false): " + example.isBorrowed());
example.borrowed();
System.out.println("Borrowed? (should be true): " + example.isBorrowed());
example.returned();
System.out.println("Borrowed? (should be false): " + example.isBorrowed());
}
}

Output

Code:


Title (should be The Da Vinci Code): The Da Vinci Code
Borrowed? (should be false): false
Borrowed? (should be true): true
Borrowed? (should be false): false

avatar
Ivi
Admin

Posts : 41
Join date : 2016-02-06

View user profile http://website-lab.forum-board.net

Back to top Go down

Re: Exercise 3

Post by Nickg on Sat May 21, 2016 12:06 am

Assignment 3:

Code:


class Marathon {
  public static int fastest(int[] times) {
    int fast = 0;
    if (times.length > 0){
      for (int i = 1; i < times.length; i++){
        if (times[i] < times[fast]){
          fast = i;
        }
      }
    }
    return fast;
  }

  public static int second(int[] times) {
    int firstPlace = fastest(times);
    int secondPlace = 0;
    if (times.length > 1){
      for (int i = 0; i < times.length; i++){
        if (times[i] > times[firstPlace]){
          if (times[i] < times[secondPlace]){
            secondPlace = i;
          }
        }
      }
    }
    return secondPlace;
  }

  public static void main(String[] arguments) {
    String[] names = {
      "Elena", "Thomas", "Hamilton", "Suzie", "Phil", "Matt", "Alex",
      "Emma", "John", "James", "Jane", "Emily", "Daniel", "Neda",
      "Aaron", "Kate"
    };

    int[] times = {
      341, 273, 278, 329, 445, 402, 388, 275, 243, 344, 412, 393, 299,
      343, 317, 265
    };

    System.out.println(names[fastest(times)] + ": " + times[fastest(times)]);
    System.out.println(names[second(times)] + ": " + times[second(times)]);
  }
}

Nickg

Posts : 2
Join date : 2016-05-15

View user profile

Back to top Go down

Re: Exercise 3

Post by Sponsored content


Sponsored content


Back to top Go down

View previous topic View next topic Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum