Java Homework Problems:
4.
• Examine AddImport.java.
– Perform the following:
– Replace the fully qualified name to access the Jlabel
component with an import statement.
– To import classes from the util package, replace multiple
import statements with a single import statement.
/*
* 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.
*/
import java.util.Calendar;
import java.util.Date;
public class AddImport {
public static void main(String args[]) {
javax.swing.JLabel label = new javax.swing.JLabel("hello");
}
}
===========================================================
5.
Exercise 1
• Examine ShoppingCart.java.
– Perform the following:
– Use the indexOf method to get the index for the space character (" ") within custName. Assign it to spaceIdx.
– Use the substring method and spaceIdx to get the first name portion of custName. Assign it to firstName and print firstName.
public class ShoppingCart {
public static void main (String[] args){
String custName = "Steve Smith";
String firstName;
int spaceIdx;
// Get the index of the space character (" ") in custName.
// Use the substring method parse out the first name and print it.
}
}
Exercise 2
Examine NameMaker.java.
• Perform the following:
– Declare String variables: firstName, middleName, lastName, and fullName
– Prompt users to enter their first, middle, and last names and read the names from the keyboard.
– Set and display the fullName as firstName+a blank char+middleName+a blank char+lastName.
public class NameMaker {
public static void main(String args[])
{
}
}
Exercise 2
• Which do you think is preferable for this scenario?
• That is, the string concatenation operator or the concat() method?
AddImport.java
|
//To import classes from the util package, replace
multiple public class AddImport { public static void main(String args[]) { } } |
Output

5. Exercise 1
ShoppingCart.java
|
public class ShoppingCart { public static void main (String[] args){ String custName = "Steve Smith"; String firstName; int spaceIdx; // Get the index of the space character (" ") in custName. |
Output

Exercise 2
NameMaker.java
|
/ //To import classes from the util package public static void main(String args[]){ System.out.println("Full Name is
: "+ fullName + )" using + operator"); |
Output

EXERCISE 2
As while using the concatenate + operator you can concatenate A string with any data type.
like you can concat numbers with strings
example: String numberOfBats = 10 + " Bats";
OUTPUT here will be: 10 Bats
But a concatenate method could only be able to concatenate strings (i.e you can't concat strings with numbers etc.)
As in this question, All three user inputs are Strings. you can easily use either concat using append or use + operator without using the concat method.
Here I will prefer the + operator method.
Java Homework Problems: 4. • Examine AddImport.java. – Perform the following: – Replace the fully qualified...
Exercise 1 • Examine ShoppingCart.java. – Perform the following: – Use the indexOf method to get the index for the space character (" ") within custName. Assign it to spaceIdx. – Use the substring method and spaceIdx to get the first name portion of custName. Assign it to firstName and print firstName. public class ShoppingCart { public static void main (String[] args){ String custName = "Steve Smith"; String firstName; int spaceIdx; // Get the...
JAVA PROGRAMMING In this final review lab from our intro course, use the Name class you created in the previous lab. In this lab, create a Student class with the following class variable: Student name: Name (Note: Name is a datatype) Student Id: String Create the getter and setter methods. In addition to these methods, create a toString() method, which overrides the object class toString() method. This override method prints the current value of any of this class object. Complete...
I have currently a functional Java progam with a gui. Its a
simple table of contacts with 3 buttons: add, remove, and edit.
Right now the buttons are in the program but they do not work yet.
I need the buttons to actually be able to add, remove, or edit
things on the table. Thanks so much.
Here is the working code so far:
//PersonTableModel.java
import java.util.List;
import javax.swing.table.AbstractTableModel;
public class PersonTableModel extends AbstractTableModel
{
private static final int...
read the code and comments, and fix the program by INSERTING the missing code in Java THank you please import java.util.Scanner; public class ExtractNames { // Extract (and print to standard output) the first and last names in "Last, First" (read from standard input). public static void main(String[] args) { // Set up a Scanner object for reading input from the user (keyboard). Scanner scan = new Scanner (System.in); // Read a full name from the user as "Last, First"....
8.26
Encapsulate the Name class. Modify the existing code shown below
to make its fields private, and add appropriate accessor methods to
the class named getFirstName, getMiddleInitial, and
getLastName.
code:
class Name {
private String firstName;
private String middleNames;
private String lastName;
//Constructor method
public Name(String firstName, String middleNames, String
lastName)
{
this.firstName = firstName;
this.middleNames = middleNames;
this.lastName = lastName;
}
//Accessor for firstName
public String getFirstName()
{
return firstName;
}
//Accessor for middleNames
public String getMiddlesNames()...
Write a program that reads a person's first and last names, separated by a space. Then the program outputs last name, comma, first name. End with newline. Example output if the input is: Maya Jones Jones, Maya import java.util.Scanner; public class SpaceReplace { public static void main (String [] args) { Scanner scnr = new Scanner(System.in); String firstName; String lastName; //answer goes here// } }
java 1. Write a method header on line three with the following specs: Returns: a boolean Name: beTrue Parameters: none public class Main { { return true; } } 2. Write a method header on line two with the following specs: Returns: a String Name: makeCapital Parameters: a String named "name" You should not be writing code on any line other than #2 public class Main { { String ans = name.toUpperCase();...
6. • Examine FlipCoin.java: – Execute the following program and observe the random number that chance generated. – If chance < 0.5, record the result as “heads”; else record the result as “tails.” – Repeat this many time. import java.util.Random; public class FlipCoin { public static void main(String[] args) { // 50% chance heads, 50% chance tails Random rand = new Random(); double chance = rand.nextDouble(); System.out.println(chance); } } Exercise 2 • Examine...
JAVA Can you make this return the first letter of first and last name capitalized? import java.io.File; import java.io.IOException; import java.util.*; public class M1 { public static void main(String[] args) { //scanner input from user Scanner scanner = new Scanner(System.in); // System.out.print("Please enter your full name: "); String fullname = scanner.nextLine(); //creating a for loop to call first and last name separately int i,k=0; String first="",last=""; for(i=0;i<fullname.length();i++) { char j=fullname.charAt(i); if(j==' ') { k=i; break; } first=first+j;...
In Java This is the method we did in class. How do I reverse the string "monday"? If I could get the string "onday" reversed (to produce "yadno" then all I have to do is append the first character to make "yadnom". import java.util.Scanner; public class Lab12Num2 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); String s=sc.nextLine(); System.out.println("The string entered was " + s); System.out.println("The string printed backwards is "+ printBackwards(s)); } public static String printBackwards(String one)...